forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstNonRepeatingCharacter.java
More file actions
63 lines (48 loc) · 1.62 KB
/
Copy pathFirstNonRepeatingCharacter.java
File metadata and controls
63 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
class Index {
int count;
int index;
public Index(int index) {
this.count = 1;
this.index = index;
}
public void incrementCount() {
this.count++;
}
}
public class FirstNonRepeatingCharacter {
static final int NO_OF_CHARS = 256;
static HashMap<Character, Index> hm = new HashMap<>(NO_OF_CHARS);
static void getCharCountArray(String str) {
for (int i = 0; i < str.length(); i++) {
if(hm.containsKey(str.charAt(i))) {
hm.get(str.charAt(i)).incrementCount();
}
else {
hm.put(str.charAt(i), new Index(i));
}
}
}
static int firstNonRepeatingCharacter(String str) {
getCharCountArray(str);
int result = Integer.MAX_VALUE, i;
for (i = 0; i < str.length(); i++) {
// If this character occurs only once and appears
// before the current result, then update the result
if (hm.get(str.charAt(i)).count == 1 && result > hm.get(str.charAt(i)).index){
result = hm.get(str.charAt(i)).index;
}
}
return result;
}
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input String: ");
String str = scanner.nextLine();
int index = firstNonRepeatingCharacter(str);
System.out.println(index == Integer.MAX_VALUE ? "Either all characters are repeating " +
" or string is empty" : "First non-repeating character is " + str.charAt(index));
}
}