-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution11.java
36 lines (31 loc) · 887 Bytes
/
Solution11.java
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
public class Solution11 {
/*
Implement an algorithm to determine if a string has all unique characters.
What if you can not use additional data structures?
*/
private boolean isUniqueChars(String inputString) {
if (inputString.length() > 256) {
return false; // if size is too long, we must meet duplicate
}
boolean[] chars = new boolean[256];
for (int i = 0; i < inputString.length(); i++) {
int val = inputString.charAt(i);
if (chars[i]) {
return false;
} else {
chars[i] = true; // we already meet this char
}
}
return true;
}
public static void main(String[] args) {
Solution11 sol = new Solution11();
String inputString = "acedfghijklmnopqxstuvwxyza";
boolean result = sol.isUniqueChars(inputString);
if (result) {
System.out.println("Find duplicate char");
} else {
System.out.println("No duplicate char");
}
}
}