-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNon_Repeating_Char.java
More file actions
34 lines (29 loc) · 989 Bytes
/
Non_Repeating_Char.java
File metadata and controls
34 lines (29 loc) · 989 Bytes
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
class Non_Repeating_Char{
static final int NO_OF_CHARS = 256;
static char count[] = new char[NO_OF_CHARS];
static void getCharCountArray(String str) {
for (int i = 0; i < str.length(); i++)
count[str.charAt(i)]++;
}
static int firstNonRepeating(String str) {
getCharCountArray(str);
int index = -1, i;
for (i = 0; i < str.length(); i++) {
if (count[str.charAt(i)] == 1) {
index = i;
break;
}
}
return index;
}
public static void main(String[] args) {
String str = "codewarriors";
int index = firstNonRepeating(str);
System.out.println(
index == -1
? "Either all characters are repeating or string "
+ "is empty"
: "First non-repeating character is "
+ str.charAt(index));
}
}