Skip to content

Commit f3ddd03

Browse files
authored
Update 424.Longest-Repeating-Character-Replacement_v1.cpp
1 parent c18d297 commit f3ddd03

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
class Solution {
1+
class Solution {
2+
vector<int>count;
23
public:
34
int characterReplacement(string s, int k)
45
{
56
int ret = 0;
67
int j = 0;
7-
vector<int>count(26);
8+
count.resize(26);
89
for (int i=0; i<s.size(); i++)
910
{
10-
while (j-i - *max_element(count.begin(), count.end()) <= k)
11-
{
12-
ret = max(ret, j-i);
13-
if (j<s.size())
14-
{
15-
count[s[j]-'A']++;
16-
j++;
17-
}
18-
else
19-
break;
20-
}
11+
while (j<s.size() && checkOK(s, j, j-i+1, k))
12+
j++;
13+
ret = max(ret, j-i);
2114
count[s[i]-'A']--;
2215
}
2316
return ret;
2417
}
18+
19+
bool checkOK(string&s, int j, int total, int k)
20+
{
21+
count[s[j]-'A']++;
22+
int maxCount = *max_element(count.begin(), count.end());
23+
if (total - maxCount <= k)
24+
return true;
25+
else
26+
{
27+
count[s[j]-'A']--;
28+
return false;
29+
}
30+
}
2531
};

0 commit comments

Comments
 (0)