Search code examples
javastringsubstringlongest-substring

How to get count of max consecutive substring ab in given string abhwasababababuqabab


How to get count of max consecutive substring ab in given string abhwasababababuqabab

Input: abhwasababababuqabab

Output: 4 how?

We have 4 consecutive ab at middle from 6th index to 13th index. At starting we have only 1 ab and at end we have 2 consecutive ab, but at middle we have 4 ab so we need to return max count of consecutive ab in the string.

public class MaxConsecutiveAbCount {
    public static int maxConsecutiveAbCount(String s) {
        int maxCount = 0;  // To store the maximum consecutive count
        int currentCount = 0;  // To store the current consecutive count

        for (int i = 0; i < s.length() - 1; i++) {
            // Check if the current and next characters form the substring "ab"
            if (s.substring(i, i + 2).equals("ab")) {
                currentCount++;
            } else {
                // Update maxCount if the current count is greater
                maxCount = Math.max(maxCount, currentCount);
                currentCount = 0;  // Reset current count for a non-"ab" substring
            }
        }

        // Update maxCount again in case the last substring is "ab"
        maxCount = Math.max(maxCount, currentCount);

        return maxCount;
    }

    public static void main(String[] args) {
        String inputString = "abhwasababababuqabab";
        int output = maxConsecutiveAbCount(inputString);
        System.out.println("Output: " + output);
    }
}

Solution

  • Your code doesn't work because every time it finds an instance of ab, it will check the next 2 characters, which would be b followed by another character. This would cause it to reset count.

    This can be easily fixed by incrementing i with i++ after you find ab to skip the check on the b.

    public class a {
        public static int maxConsecutiveAbCount(String s) {
            int maxCount = 0;  // To store the maximum consecutive count
            int currentCount = 0;  // To store the current consecutive count
    
            for (int i = 0; i < s.length() - 1; i++) {
                // Check if the current and next characters form the substring "ab"
                if (s.substring(i, i + 2).equals("ab")) {
                    currentCount++;
                    i++; // <--- ADD THIS LINE
                } else {
                    // Update maxCount if the current count is greater
                    maxCount = Math.max(maxCount, currentCount);
                    currentCount = 0;  // Reset current count for a non-"ab" substring
                }
            }
    
            // Update maxCount again in case the last substring is "ab"
            maxCount = Math.max(maxCount, currentCount);
    
            return maxCount;
        }
    
        public static void main(String[] args) {
            String inputString = "abhwasababababuqabab";
            int output = maxConsecutiveAbCount(inputString);
            System.out.println("Output: " + output);
        }
    }