I need an updated version of my code which could return me the maximum number of the contiguous duplicated characters in a String e.g. in the case we have as an input the string "aabbbcccccd"
then the result should be 5, since we have 5 c.
The problem with my code is that it will return 4.
Note that in the case we have the input "abccbbb
", the result should be 3 and not 4, since the strings contains 3 contiguous duplicated characters (the c).
private int countRepeatedChars(String password)
{
int maxNumberofRepeatedChars = 0;
int counterOfSameChar = 0;
boolean foundMoreThanOneChar = false;
char ch, nextCh;
for (int i = 0; i < password.length()-1; i++)
{
ch = password.charAt(i);
nextCh = password.charAt(i + 1);
if ((int) ch == ((int) nextCh))
{
counterOfSameChar++;
} else
{
maxNumberofRepeatedChars = Math.max(counterOfSameChar, maxNumberofRepeatedChars);
counterOfSameChar = 0;
foundMoreThanOneChar = true;
}
}
if (foundMoreThanOneChar)
return maxNumberofRepeatedChars;
else
return counterOfSameChar;
}