“氣球” 的最大數(shù)量 替換子串得到平衡字符串 情感豐富的文字
1189. “氣球” 的最大數(shù)量
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);因?yàn)榛疽獌蓚€(gè)
return Math.min(con1, con2);
1234. 替換子串得到平衡字符串
int n = s.length();采用滑動(dòng)窗口
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']--;進(jìn)去了
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;統(tǒng)計(jì)相等
char ch = s.charAt(i);
while(i < s.length() && s.charAt(i) == ch) {
cnti++;
i++;
}
int cntj = 0;統(tǒng)計(jì)相等
while(j < word.length() && word.charAt(j) == ch) {j的也要跟i的相等
cntj++;
j++;
}
if(cnti < cntj) {擴(kuò)張后沒有擴(kuò)張前長,顯然false
return false;
}
if(cntj != cnti && cnti < 3) {因?yàn)閿U(kuò)張至少得3個(gè)
return false;
}
}
return i == s.length() && j == word.length();有沒有到最后
}
鏈接:https://www.dianjilingqu.com/622314.html