-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1p4.java
More file actions
49 lines (44 loc) · 1.2 KB
/
Copy pathq1p4.java
File metadata and controls
49 lines (44 loc) · 1.2 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
import java.util.*;
import java.lang.*;
public class q1p4{
public static void main(String[] args){
String test = "qqwertwerty";
System.out.println(method(test));
System.out.println(method2(test));
}
// T: O(n)
// S: O(1)
// A:
public static boolean method(String test){
int[] cnt = new int[26];
for(char c : test.toCharArray()){
cnt[c-'a']++;
}
boolean odd = false;
for(int i : cnt){
if(i%2 == 1 && odd == true)
return false;
else if(i%2 == 1)
odd = true;
}
return true;
}
// T: O(n)
// S: O(1) better
// A:
public static boolean method2(String test){
int[] cnt = new int[26];
int oddCounter = 0;
for(char c : test.toCharArray()){
cnt[c-'a']++;
if(cnt[c-'a']%2 == 1)
oddCounter++;
else
oddCounter--;
}
return oddCounter <= 1;
}
// We can also use bit vector, no need to count the real number,
// just know the final answer is 0 or 1 that should be fine.
// Bad thing is , it is not very straight forward.
}