-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoPlan0605
70 lines (65 loc) · 1.81 KB
/
AlgoPlan0605
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#count good strings
##key idea:
- use maps to store counts of different characters
- loop over the string
class Solution {
public boolean isGoodString(String s){
Map<Character,Integer> map = new HashMap();
for (int k = 0; k < s.length();k++){
if (!map.containsKey(s.charAt(k))){
map.put(s.charAt(k),1);
}else{
map.put(s.charAt(k), map.get(s.charAt(k))+1);
}
}
for (int k : map.values()){
if (k >1){
return false;
}
}
return true;
}
public int countGoodSubstrings(String s) {
String temp = "";
int count = 0;
for (int k = 0; k<= s.length()-3 ; k++){
temp = s.substring(k, k+3);
if (isGoodString(temp)){
count++;
}
}
return count;
}
}
#create k palindrome strings
##key idea:
- if the # of characters <k, cannot;
- if the # of characters who have odd counts > k, which means some of the characters cannot be placed at centers or in pair, cannot;
- all the other cases, it is plausible.
class Solution {
public boolean canConstruct(String s, int k) {
if (s.length() <k){
return false;
}
Map<Character, Integer> map = new HashMap();
char temp;
for (int i = 0;i < s.length();i++){
temp = s.charAt(i);
if (map.containsKey(temp)){
map.put(temp,map.get(temp)+1);
}else{
map.put(temp,1);
}
}
int notoddcount=0;
for (int count: map.values()){
if (count %2 !=0 ){
notoddcount++;
}
}
if (notoddcount >k){
return false;
}
return true;
}
}