Share me the right approach i have tried without changing original string

@Yuganshu your logic is wrong

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’.
    If you are not able to write a code, I will help.
    Try yourself first

not clear with it … i have tried this way can you plz share the code.

Scanner scn = new Scanner(System.in);
	int k = scn.nextInt();
	int i=0; // beginning of window
	int j=k; // end of window
	String[] s = new String[5];
	for(int c = 0 ; c <= s.length - 1 ; c++) {
		s[c] = scn.next().toString();
	}
	for(String val : s ) {
		System.out.println(val);
	}

// A window
int count = 0;
for( j=k ; j <= s.length -1 ; j++) {
if(s[j] == β€œa”) {

		}
		else if (s[j] == "b") {
			if(count < k ) {
			s[j] = "a";	
			count ++;
			}
			
		}
		int length = j-i+1;
	}

Dry run this code
correct Code