-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1p1.java
More file actions
51 lines (48 loc) · 1.4 KB
/
Copy pathq1p1.java
File metadata and controls
51 lines (48 loc) · 1.4 KB
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
import java.util.*;
public class q1p1{
public static void main(String[] agrs){
String testString = "qweratyuioplkjhgfdsazxcvbnm";
boolean ans = false;
// ans = hashSetWay(testString);
ans = singleIntWay(testString);
if(ans == false)
System.out.println(false);
else
System.out.println(true);
}
// assumption: ASCII
// time complexity: O(n)
// space complexity: O(n)
public static boolean hashSetWay(String testString){
if(testString.length()>=128){
return false;
}
Set<Character> hs = new HashSet<>();
for(char c : testString.toCharArray()){
if(!hs.add(c)){
return false;
}
}
return true;
}
// time complexity: O(n)
// space complexity: O(1)
public static boolean singleIntWay(String testString){
if(testString.length()>=128){
return false;
}
int checker = 0;
for(int i = 0; i< testString.length(); i++){
int buf = testString.charAt(i) - 'a';
if((checker&(1<<buf))!=0){
return false;
}
checker |= (1<<buf);
}
return true;
}
// if no other data structure can be use:
// T: O(n^2), S: O(1)
// T: O(nlgn) if we can sort the string. But sort algorithms usually
// take more space.
}