-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHasAllUniqueChars.java
36 lines (32 loc) · 1.05 KB
/
HasAllUniqueChars.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
package arraysandstrings;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* HasAllUniqueChars
* https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_01_Is_Unique
* Difficulty : Easy
* Related Topics : Array, String
*
* created by Cenk Canarslan on 2021-10-24
*/
public class HasAllUniqueChars {
@Test
public void check() {
HasAllUniqueChars hauc = new HasAllUniqueChars();
assertTrue(hauc.hasAllUnique("cenkC"));
assertFalse(hauc.hasAllUnique("cenkCan"));
}
private boolean hasAllUnique(String str) {
boolean[] ascii_char_set = new boolean[128];
for (int i = 0; i < str.length(); i++) {
int value = str.charAt(i);
// System.out.println("char = '" + (char) value + "', value = " + value + ", i = " + i);
if (ascii_char_set[value]) {
return false;
}
ascii_char_set[value] = true;
}
return true;
}
}