Hey can you please take a look at my code , I have no idea where is the bug
2 test cases failing?
Okay ,so how should i approach this??
Not able to figure out the solution
Approach - Two pointer approach
You can solve this problem in O(n) time using the two pointer approach.
- Make two variabes , say i and j .
- i defines the beginning of a window and j defines its end.
- Start i from 0 and j from k.
- Let’s talk about the singular case when we are considering the max window for only 'a’s and consider only the swapping of b-> a. If we are able to get the answer for max window of consecutive 'a’s , we can simply implement the same algo for the max ‘b’ window as well.
- So we started i from 0 and j from k.
- Move j ahead freely as long as there are ‘a’ characters at s[ j ] position.
- Maintain a count variable which counts the number of swaps made or the number of 'b’s in our A window.
- If you encounter a ‘b’ char at s[ j ] position , increment the count variable. Count should never exceed k .
- Take the size of the window at every point using length = j - i + 1;
- Compute the max size window this way and do the same for ‘b’ as well.
- Output the maximum size window of ‘a’ and ‘b’.
Hey is this some kind of “Sliding window technique” , or if it is “two pointer algorithm” , should i study these things from somewhere else as these things were mentioned no where in the course , or will they be covered later please tell what should do
Also in this problem if we are considering i at 0 and j at k what about characters in btw them how are we taking them into accounts and making swaps for them??
Still cnnot apply this ,need a little more help
@bhavik911
Sliding window technique and two pointer algorithm
it’s not a big concepts .
it’s a simple logic.
Q. we are considering i at 0 and j at k
Answer : i and j both set at 0.
if you are not able to write a code . I will send you. dry run the code . its simple and clear
everything
Hey I did the same thing but 2 test cases are not ruuning , can you please heck this code out
please Send me updated code
Please check this code
package Strings;
import java.util.Scanner;
public class SanketAndStrings {
public static void main(String[] args) {
Scanner scanner = new Scanner( System.in);
int k = scanner.nextInt();
String str = scanner.next();
System.out.println(Math.max(countForA(str,k),countForB(str,k)));
}
public static int countForA(String str, int k){
int i=0;
int j = 0;
int max = 0;
int count = 0;
while(i<str.length() &&j<str.length()){
char ch = str.charAt(j);
if(ch=='b' && count<k){
j++;
count++;
}else if(ch=='a' ){
j++;
}else{
if(j-i>max){
max= j-i;
}
i = i+j;
j=0;
count =0;
}
}
return Integer.max(j-i,max);
}
public static int countForB(String str, int k){
int i=0;
int j = 0;
int max = 0;
int count = 0;
while(i<str.length() &&j<str.length()){
char ch = str.charAt(j);
if(ch=='a' && count<k){
j++;
count++;
}else if(ch=='b'){
j++;
}else{
if(j-i>max){
max= j-i;
}
i = i+j;
j=0;
count =0;
}
}
return Integer.max(j-i,max);
}
}
Debug your code
for this input
1
abababb
correct output : 4
public static int maximumcharchange(String str, char ch, int k) {
int max = 1;
int l = 0;
int r = 0;
int count = 0;
while (r < str.length()) {
if (str.charAt( r) != ch)
count++;
while (count > k) {
if (str.charAt(l) != ch)
count–;
l++;
}
max = Math.max(max, r - l + 1);
r++;
}
return max;
}
output should be : System.out.println(Math.max(maximumcharchange(str, ‘a’, k), maximumcharchange(str, ‘b’, k)))