0

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;
}
4

5 回答 5

4

Your code is fine, but the answer to the number of duplicated characters is counterOfSameChar+1, not counterOfSameChar. This is because it looks for contiguous identical characters, so "cc" counts 1, "ccc" counts 2 etc.

于 2013-06-07T08:31:25.697 回答
3

When the next character is the same as the first character, you increase counterOfSameChar. The first time this happens, you set this to 1 although it is already the second occurance. You should initiate counterOfSameChar with 1.

于 2013-06-07T08:31:42.590 回答
2

Shortest possible code?

private int countRepeatedChars(String password) {
    int countMax = 0;
    int count = 1;
    int pos = 1;

    if(password != null && password.length() > 1)
    {
        for(pos = 1;pos < password.length();pos++)
        {
            if(password.charAt(pos) == password.charAt(pos - 1))
            {
                count++;
            }
            else
            {
                count = 1;
            }

            if(count > countMax)
            {
                countMax = count;
            }
        }
    }
    else if(password != null)
    {
        countMax = 1;
    }

    return countMax;
}
于 2013-06-07T08:40:39.630 回答
0

You count the pairs os characters which gives the correct answer...

To count the duplicated characters you have to count single characters, e.g.:

ch = password.charAt(i);
prevCh = password.charAt(i - 1);
nextCh = password.charAt(i + 1);
if (ch == nextCh || ch == prevCh) {

Please also check that you're in the bounds of the String (first and last character).

于 2013-06-07T08:31:31.607 回答
0
    Pattern pattern = Pattern.compile("(?<=(.))(?!\\1)");
    String[] count = pattern.split(test,0);
    for(String str : count){
        runningString.append("("+str.length()+")"+str.charAt(0));
    }       
    System.out.println(runningString.toString());
于 2013-11-20T19:05:26.543 回答