-
Notifications
You must be signed in to change notification settings - Fork 94
/
IsUnique.java
55 lines (51 loc) · 1.07 KB
/
IsUnique.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package chapter01ArraysAndStrings;
/**
*
* Problem: Implement an algorithm to determine if a string has all unique
* characters. No extra data structure.
*
*/
public class IsUnique {
/**
* O(N) O(1)
*/
public boolean isUnique1(String s) {
if (s == null || s.length() > 256) {
return false;
}
boolean[] map = new boolean[256];
char[] chars = s.toCharArray();
for (char c : chars) {
if (map[c]) {
return false;
}
map[c] = true;
}
return true;
}
/**
* Assume the string only uses lowercase letters. Use just a single
* int(32bits) to save more space complexity
*/
public boolean isUnique2(String s) {
if (s == null || s.length() > 26) {
return false;
}
int checker = 0;
char[] chars = s.toCharArray();
for (char c : chars) {
int val = c - 'a';
if ((checker & (1 << val)) > 0) {
return false;
}
checker |= (1 << val);
}
return true;
}
public static void main(String[] args) {
IsUnique iu = new IsUnique();
String s = "abcb";
System.out.println(iu.isUnique1(s));
System.out.println(iu.isUnique2(s));
}
}