String First Questions cannot understand how to solve it

String First Questions cannot understand how to solve it

@ashishsoni1412 You this 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’.

plz provide a solution sir

import java.util.*; public class Main { public static void main(String args[]) { Scanner sc =new Scanner(System.in); String S=sc.nextLine(); int L=0;int R=0; HashSet seen=new HashSet(); int max=0; while(R<S.length()){ char c=S.charAt®; if(seen.add©){ max=Math.max(max,R-L+1); R++; }else{ while(S.charAt(L)==R){ seen.remove(S.charAt(L)); L++; }seen.remove©; L++; } } return max; } }

@ashishsoni1412 You can refer my code :
``

import java.util.Scanner;

public class Main {

    // Function to count the length of window which can be made of char ch with <= k
    // swaps
    static int countMaxWindowSize(String s, char ch, int k) {
        int i = 0; // Left pointer
        int j = 0; // Right pointer

        // First move the right pointer forward by k steps.
        // If the character is already ch , do not count a swap and move freely

        int c = 0; // Variable to count the swaps so far

        int ans = 0; // Variable to store the final answer

        for (; c < k && j < s.length() - 1; j++) {
            if (s.charAt(j) != ch) {
                // If s.charAt(j) is not ch then count it as a swap and move forward
                c++;
            }
            if (c == k) {
                // If no of swaps has reached k, stop moving j any more forward
                break;
            }
        }

        while (i < j) {

            // Move j ahead if next element is ch as it doesn't count as a swap
            while (j < s.length() - 1 && s.charAt(j + 1) == ch) {
                j++;
            }

            // Store the maximum length of all windows
            int currentLength = j - i + 1;
            ans = Math.max(ans, currentLength);

            // Move left pointer by one to slide the window
            i++;

            // If the char at previous position of left pointer was not ch, then that
            // position must
            // have counted as a swap earlier. Now we have a free swap available.
            // Iterate right pointer forward to use that one free swap
            if (j < s.length() - 1 && s.charAt(i - 1) != ch) {
                j++;
            }
        }

        return ans;
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        String s = sc.next();

        if (k >= s.length()) {
            // If k is larger than s.length() then we can swap all the elements to either A
            // or B
            // and obtain the answer equal to length of string
            System.out.println(s.length());
            return;
        }

        // First let us check for longest perfect string of A's then we will find the
        // same for B's and compare
        int ansForA = countMaxWindowSize(s, 'a', k);

        // Now we do the same for B's
        int ansForB = countMaxWindowSize(s, 'b', k);

        // Final answer is max of the two answers obtained above
        System.out.println(Math.max(ansForA, ansForB));
    }
}

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.