I want the left, right and mid walla solution. I’m unable to find it please help.
Did this question on my own but getting TLE
link to my code
Sanket and strings
hi @Yuvraj_Luhach
This problem can be solved with help of two pointers. Let the first pointer is l and the second pointer is r. Then for every position l we will move right end r until on the substring si.si + 1… sr it is possible to make no more than k swaps to make this substring beautiful. Then we need to update the answer with length of this substring and move l to the right.
Hi, Sir I’m unable to understand this explanation could you send me the code so that I can look over and understand it better with your explanation .
you are using brute force approach try optimize your code through the upon explanation its better for you to solve it own your own.
Sir , I can’t understand the explanation could you please provide the code.
ok
import java.util.Scanner;
public class SanketAndString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
int k=scn.nextInt();
String str = scn.next();
int left=0, ans=0;
int[] count = {0,0};
for (char c: str.toCharArray()) {
count[c-'a']++;
if(Math.min(count[0], count[1]) > k) {
count[str.charAt(left)-'a']--;
left++;
} else {
ans++;
}
}
System.out.println(ans);
}
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.