-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path424.longest-repeating-character-replacement.cpp
77 lines (77 loc) · 1.61 KB
/
424.longest-repeating-character-replacement.cpp
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
71
72
73
74
75
76
77
/*
* @lc app=leetcode id=424 lang=cpp
*
* [424] Longest Repeating Character Replacement
*
* https://leetcode.com/problems/longest-repeating-character-replacement/description/
*
* algorithms
* Medium (44.56%)
* Total Accepted: 34.6K
* Total Submissions: 77.5K
* Testcase Example: '"ABAB"\n2'
*
* Given a string s that consists of only uppercase English letters, you can
* perform at most k operations on that string.
*
* In one operation, you can choose any character of the string and change it
* to any other uppercase English character.
*
* Find the length of the longest sub-string containing all repeating letters
* you can get after performing the above operations.
*
* Note:
* Both the string's length and k will not exceed 104.
*
* Example 1:
*
*
* Input:
* s = "ABAB", k = 2
*
* Output:
* 4
*
* Explanation:
* Replace the two 'A's with two 'B's or vice versa.
*
*
*
*
* Example 2:
*
*
* Input:
* s = "AABABBA", k = 1
*
* Output:
* 4
*
* Explanation:
* Replace the one 'A' in the middle with 'B' and form "AABBBBA".
* The substring "BBBB" has the longest repeating letters, which is 4.
*
*
*
*
*/
class Solution {
public:
int callme(vector<int>& dp){
int maxx = 0;
for(int c=0; c<26; c++)
maxx = max(maxx, dp[c]);
return maxx;
}
int characterReplacement(string s, int K) {
vector<int> cnt(26, 0);
int ans = 0, i = 0;
for(int j = 0; j<s.size(); j++){
cnt[s[j]-'A']++;
while(i<j && (j-i+1-callme(cnt))>K)
--cnt[s[i++]-'A'];
ans = max(ans, j-i+1);
}
return ans;
}
};