-
Notifications
You must be signed in to change notification settings - Fork 6
/
IsUnique.java
44 lines (37 loc) · 1.13 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
package jobinterview;
import java.util.*;
import java.io.*;
/**
*
* @author dsegal
* IS Unique - find if string is unique chars
*/
public class IsUnique {
public static boolean isUniqueUsingHash(String word) {
char[] chars = word.toCharArray();
Set<Character> set = new HashSet<Character>();
for (char c : chars)
if (set.contains(c))
return false;
else
set.add(c);
return true;
}
public static boolean isUniqueUsingSort(String word) {
char[] chars = word.toCharArray();
if (chars.length <= 1) return true;
Arrays.sort(chars);
char temp = chars[0];
for (int i = 1; i < chars.length; i++) {
if (chars[i] == temp) return false;
temp = chars[i];
}
return true;
}
public void runTests(){
System.out.print("When running `Word`: ");
System.out.println(isUniqueUsingHash("Word") ? "Unique" : "Not Unique");
System.out.print("When running `Nootunique`: ");
System.out.println(isUniqueUsingSort("Nootunique") ? "Unique" : "Not Unique");
}
}