“气球” 的最大数量 替换子串得到平衡字符串 情感丰富的文字()

1189. “气球” 的最大数量

int[] pre = new int[26];
for(int i = 0; i < text.length(); i++) {
pre[text.charAt(i) – ‘a’]++;
}
int b = pre[‘b’ – ‘a’];
int a = pre[‘a’ – ‘a’];
int n = pre[‘n’ – ‘a’];
int l = pre[‘l’ – ‘a’];
int o = pre[‘o’ – ‘a’];
int con1 = Math.min(Math.min(b, a), n);分别求
int con2 = Math.min(l / 2, o / 2);因为基本要两个
return Math.min(con1, con2);

1234. 替换子串得到平衡字符串

int n = s.length();采用滑动窗口
int[] count = new int[26];
for(int i = 0; i < n; i++) {
count[s.charAt(i) – ‘A’]++;
}
int left = 0, right = 0;
int ans = n;
int ave = n / 4;
while(right < n) {
count[s.charAt(right) – ‘A’]–;进去了
while(left < n && count[‘Q’ – ‘A’] <= ave && count[‘W’ – ‘A’] <= ave && count[‘E’ – ‘A’] <= ave && count[‘R’ – ‘A’] <= ave) {
ans = Math.min(ans, right – left + 1);
count[s.charAt(left) – ‘A’]++;出来了
left++;
}
right++;
}
return ans;

809. 情感丰富的文字

public boolean dnn(String s, String word) {
int i = 0;双指针
int j = 0;
while(i < s.length() && j < word.length()) {
if(s.charAt(i) != word.charAt(j)) {
return false;
}
int cnti = 0;统计相等
char ch = s.charAt(i);
while(i < s.length() && s.charAt(i) == ch) {
cnti++;
i++;
}
int cntj = 0;统计相等
while(j < word.length() && word.charAt(j) == ch) {j的也要跟i的相等
cntj++;
j++;
}
if(cnti < cntj) {扩张后没有扩张前长,显然false
return false;
}
if(cntj != cnti && cnti < 3) {因为扩张至少得3个
return false;
}
}
return i == s.length() && j == word.length();有没有到最后
}

————————

1189. “气球” 的最大数量

int[] pre = new int[26];
for(int i = 0; i < text.length(); i++) {
pre[text.charAt(i) – ‘a’]++;
}
int b = pre[‘b’ – ‘a’];
int a = pre[‘a’ – ‘a’];
int n = pre[‘n’ – ‘a’];
int l = pre[‘l’ – ‘a’];
int o = pre[‘o’ – ‘a’];
int con1 = Math.min(Math.min(b, a), n);分别求
int con2 = Math.min(l / 2, o / 2);因为基本要两个
return Math.min(con1, con2);

1234. 替换子串得到平衡字符串

int n = s.length();采用滑动窗口
int[] count = new int[26];
for(int i = 0; i < n; i++) {
count[s.charAt(i) – ‘A’]++;
}
int left = 0, right = 0;
int ans = n;
int ave = n / 4;
while(right < n) {
count[s.charAt(right) – ‘A’]–;进去了
while(left < n && count[‘Q’ – ‘A’] <= ave && count[‘W’ – ‘A’] <= ave && count[‘E’ – ‘A’] <= ave && count[‘R’ – ‘A’] <= ave) {
ans = Math.min(ans, right – left + 1);
count[s.charAt(left) – ‘A’]++;出来了
left++;
}
right++;
}
return ans;

809. 情感丰富的文字

public boolean dnn(String s, String word) {
int i = 0;双指针
int j = 0;
while(i < s.length() && j < word.length()) {
if(s.charAt(i) != word.charAt(j)) {
return false;
}
int cnti = 0;统计相等
char ch = s.charAt(i);
while(i < s.length() && s.charAt(i) == ch) {
cnti++;
i++;
}
int cntj = 0;统计相等
while(j < word.length() && word.charAt(j) == ch) {j的也要跟i的相等
cntj++;
j++;
}
if(cnti < cntj) {扩张后没有扩张前长,显然false
return false;
}
if(cntj != cnti && cnti < 3) {因为扩张至少得3个
return false;
}
}
return i == s.length() && j == word.length();有没有到最后
}