Skip to content

Commit 7bbc24f

Browse files
authored
Sanket And Strings Solution Added
1 parent 6bd1576 commit 7bbc24f

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Hacker Blocks/Sanket And Strings.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Hacker Blocks */
2+
/* Title - Sanket And Strings */
3+
/* Created By - Akash Modak */
4+
/* Date - 24/6/2020 */
5+
6+
// Sanket has a string consisting of only 'a' and 'b' as the characters. Sanket describes perfectness of a string as the maximum length substring of equal characters. Sanket is given a number k which denotes the maximum number of characters he can change. Find the maximum perfectness he can generate by changing no more than k characters.
7+
8+
// Input Format
9+
// The first line contains an integer denoting the value of K. The next line contains a string having only ‘a’ and ‘b’ as the characters.
10+
11+
// Constraints
12+
// 2 ≤ N ≤ 10^6
13+
14+
// Output Format
15+
// A single integer denoting the maximum perfectness achievable.
16+
17+
// Sample Input
18+
// 2
19+
// abba
20+
// Sample Output
21+
// 4
22+
// Explanation
23+
// We can swap the a's to b using the 2 swaps and obtain the string "bbbb". This would have all the b's and hence the answer 4.
24+
// Alternatively, we can also swap the b's to make "aaaa". The final answer remains the same for both cases.
25+
26+
#include<bits/stdc++.h>
27+
using namespace std;
28+
29+
#define ll long long int
30+
31+
int main()
32+
{
33+
34+
int k;
35+
cin >> k;
36+
string str;
37+
cin >> str;
38+
int freq[2] = {0};
39+
ll n = str.length();
40+
ll ans = 0, left = 0;
41+
for (ll i = 0; i < n; i++)
42+
{
43+
char temp = str[i];
44+
freq[temp - 'a']++;
45+
if (min(freq[0], freq[1]) > k)
46+
{
47+
freq[str[left] - 'a']--;
48+
left++;
49+
}
50+
else
51+
ans++;
52+
}
53+
cout << ans << endl;
54+
return 0;
55+
}

0 commit comments

Comments
 (0)