-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
IsUnique.java
51 lines (46 loc) · 1.63 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
package com.ctci.arraysandstrings;
/**
* @author rampatra
* @since 18/11/2018
*/
public class IsUnique {
private static boolean hasAllUniqueCharacters(String str) {
if (str == null || str.length() > 128) return false;
boolean[] charSet = new boolean[128]; // assuming the string contains only ASCII characters
for (int i = 0; i < str.length(); i++) {
int charVal = str.charAt(i);
if (charSet[charVal]) {
return false;
}
charSet[charVal] = true;
}
return true;
}
private static boolean hasAllUniqueCharactersWhenStringContainsAllLowercase(String s) {
int checker = 0;
for (int i = 0; i < s.length(); i++) {
int charValue = s.charAt(i) - 'a';
if ((checker & (1 << charValue)) > 0) {
return false;
}
checker |= (1 << charValue);
}
return true;
}
public static void main(String[] args) {
String s = "ram";
System.out.println(hasAllUniqueCharacters(s));
s = "rama";
System.out.println(hasAllUniqueCharacters(s));
s = "ramA";
System.out.println(hasAllUniqueCharacters(s));
System.out.println("-------");
s = "ram";
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
s = "rama";
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
// not working as the input contains different cases
s = "ramA";
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
}
}