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

62 Views Asked by At

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);
    }
}
2

There are 2 best solutions below

0
RecoilGaming On BEST ANSWER

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);
    }
}
0
moyu-sharo On

You may use regex

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {
    public static void main(String[] args) {
    
        // Find the first occurrence of any longest, repeated substring
            
        String s = "abhwasababababuqabab";
            
        Pattern pattern = Pattern.compile("((.+?)\\2+)");
        Matcher matcher = pattern.matcher(s);
        if (matcher.find() ) {
            System.out.println(matcher.group(2) + " in " + matcher.group(1));
            String text = matcher.group(1);
            String subtext = matcher.group(2);
            pattern = Pattern.compile(subtext);
            matcher = pattern.matcher(text);
            int c = 0;
            while(matcher.find()) {
               c++;
            }
            System.out.println(subtext + " occurs " + c + " times");    
        }
    }
}

Output:

ab in abababab
ab occurs 4 times

But if you only want to determine occurrence for 'ab' edit the code above with the following line for the regex pattern:

Pattern pattern = Pattern.compile("((ab)\\2+)");