Help fix the cycle.

It is necessary to find a substring of maximum length consisting of the same character in a row.

For example, there is the string "acccabcfbbffffffcccc" , you need to print the number 6, because ffffff is the maximum substring.

Make it necessary in one cycle.

The beginning is:

public static int getMaxCharInSubstring(String inputString) { if (inputString.isEmpty()) { return -1; } int countCharsInSubString = 0; int countCharsInSubStringTemp = 0; inputString = inputString.toUpperCase(); for (int i = 0; i < inputString.length() - 1; ++i) { if (inputString.charAt(i) == inputString.charAt(i + 1)) { countCharsInSubStringTemp +=1; ++i; if (countCharsInSubStringTemp > countCharsInSubString) { countCharsInSubString = countCharsInSubStringTemp; } } } return countCharsInSubString; } 
  • What is wrong with the beginning? - default locale
  • Is your code not working? It works, but not so? Produces not the result that is expected? Describe your problem in more detail. - Enikeyschik

3 answers 3

If you use a regular expression, you get a very beautiful and simple algorithm:

 import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String text = "acccabcfbbffffffcccc"; int maxLen = 0; Matcher m = Pattern.compile("(.)\\1+").matcher(text); while (m.find()) { String sub = m.group(); System.out.println(sub); if (sub.length() > maxLen) { maxLen = sub.length(); } } System.out.println("\nmaxLen: " + maxLen); } } 

Console:

 ccc bb ffffff cccc maxLen: 6 

Regular (.)\\1+ says that we are looking for any character, after which immediately there is 1 or more of the same characters.

    The problem you had in the condition. Check if the character is equal to the next character countCharsInSubStringTemp buffer countCharsInSubStringTemp incremented by 1, but as soon as we determine that the next character is not equal to the previous one, then countCharsInSubString< countCharsInSubStringTemp , if so, then set the maximum number of characters equal to countCharsInSubStringTemp and countCharsInSubStringTemp :

     public static int getMaxCharInSubstring(String inputString) { if (inputString.isEmpty()) { return -1; } int countCharsInSubString = 0; int countCharsInSubStringTemp = 0; inputString = inputString.toUpperCase(); int length = inputString.length(); for (int i = 0; i < length; i++) { if (inputString.charAt(i) == inputString.charAt(i + 1)) { countCharsInSubStringTemp +=1; }else if (countCharsInSubStringTemp > countCharsInSubString) { countCharsInSubString = countCharsInSubStringTemp; countCharsInSubStringTemp=0; } } } return countCharsInSubString; } 
    • What do you dislike about my answer? - Sanaev
    • I did not like it. No comments, explanations of what was wrong and what has changed either. Useless answer. - Enikeyschik
    • Also harmful. It is not necessary to use functions in the definition of a cycle. - Enikeyschik
    • @ Enikeyschik I corrected the author. it already let him understand, the algorithm is simple. I see no reason to explain it. And yes, I would solve this problem using regular expressions. - Sanaev
    • @ Enikeyschik corrected, please change your rating - Sanaev

    It turned out like this:

      int countCharsInSubString = 0; int countCharsInSubStringTemp = 0; for (int i = 0; i < inputString.length() - 1; ++i) { if (inputString.charAt(i) == inputString.charAt(i + 1)) { countCharsInSubStringTemp += 1; } else if (countCharsInSubStringTemp > countCharsInSubString) { countCharsInSubString = countCharsInSubStringTemp; countCharsInSubStringTemp = 0; } } if (countCharsInSubStringTemp > countCharsInSubString) { return (countCharsInSubStringTemp + 1); } else return (countCharsInSubString + 1); }