please explain the problem not able to understand it
What is substring of equal characters?
There is a string which consists of only a and b. A substring is a contiguous sequence of characters within a string. Our aim is to generate substrings of a and b by swapping characters such that the lengths of substrings of either a or b is maximum possible. The only constraint we have here is that only k swaps are allowed. So, you have to tell the maximum possible length of the substrings that can be generated. By swapping, we mean that a can be replaced by b and b can be replaced by a.
For example:
Consider the following string: abba and k = 2
So, we can make only two swaps
abba (swaps = 0)
aaba (swaps = 1)
aaaa (swaps = 2)
Thus, the maximum length of substring is 4.
Consider the string: ababab and k=2
ababab (swaps = 0)
aaabab (swaps =1)
aaaaab (swaps = 2)
Thus, the maximum length of substring is 5.
hey can you show the algorithm for this?also is there any video for this? i am confused like hell
The logic for solving this problem is a simple one, we begin with 2 pointers left and right, we freeze left and increase right till it is possible to make string from left to right of one character once number of different character exceeds k, we move left pointer till it becomes less than k and then we freeze left pointer and move right and this process continues till right reaches n. We do this because this way we can find the maximum solution for each left.
Take two-pointer l and r to mark the left and right index of the string under consideration.
starting from l=0,r=0,max=0,count=0.
repeat until r <n
3.1. increase the count whenever you find a different character(by different we mean if we are forming a string of an only, then b is different).
3.2. while count is greater than k,
3.2.1. decrement the count by one if the element at lth index is different.
3.2.1. increment l.
3.3. Compare max with count for maximum value.
3.4. increment r.
left act as left pointer and i as right pointer
This is the code ,please see https://ide.codingblocks.com/s/226002
shouldn’t we should update ans to 0 whenever we increment left???and we should calculate the maximum value of ans.
also count[str[l] - ‘a’]–;
how this line is perfectly decrementing the value>k in count array
sorry bro ,i cant intervene