Search code examples
javadebuggingresponse

Find length of the longest contiguous subsequence of the same character


I am trying to find the longest repetitive subsequence of contiguous characters from a string.

public int longestRep(String str) {

}

When you call the method with

longestRep("ccbbbaaaaddaa"); //Should return 4

The code I have used thus far is;

public static int longestRep(String str)
{
    int currLen = 1; // Current length of contiguous chars being held in str
    char currLet = ' '; // Current Letter *NOT NEEDED FOR CODINGBAT
    char maxLet = ' '; // Maximum length letter *NOT NEEDED FOR CODINGBAT
    int maxLen = 0; // Maximum length of contiguous chars being held in str
    //int maxCount = 0; // Highest count of contiguous chars being held in str
    int currPos = 0; // Track where in str we are at
    int strLen = str.length(); // Length of str;
    for(currPos = 0; currPos < strLen -1 ; currPos++)
    {
        currLet = str.charAt(currPos);
        //System.out.println("Curr char: "+currLet+"  Next Char: "+str.charAt(currPos+1));
        if(currLet == str.charAt(currPos+1))
        {
            currLen++;
        }
        if(currLen > maxLen)
        {
            maxLen = currLen;
            //System.out.println("Max len: "+maxLen+"  Curr Len: "+currLen);
            //maxLet = currLet;
            currLen = 1;
        }
        boolean atBeginning = true;
        if(currPos == 0)
        {
            atBeginning = true;
        }
        else if(currPos != 0)
        {
            atBeginning = false;
        }
        if(atBeginning == false) //if not at the beginning of the string
        {
            if(currLet != str.charAt(currPos+1) && currLet == str.charAt(currPos-1))
            {
                currLen++;
            }
        }
        if(currLen > maxLen)
        {
            maxLen = currLen;
            currLen = 1;
        }
    }

    return maxLen;
  }
public static void main(String args[])
{
    int result = longestRep("abcdeeefeeeefppppppp");
    System.out.println(result);
}

However, I am getting invalid responses and am unsure what I have done wrong. I am a newbie to Java. Some of the code I have only just written and may/may not be being used.


Solution

  • I can't make heads nor tails of any thing after boolean atBeginning = true; and frankly, it doesn't seem to be required...

    Your basic logic should follow something like...

    if character_at_current_position is equal to next_character then
        currLen = currLen + 1
    else if currLen is greater then maxLen then
        maxLen = currLen
        currLen = 1
    else currLen = 1
    

    You would also need to perform another check for currLen is greater then maxLen outside the loop to account for any changes the last character might make

    More like...

    for (currPos = 0; currPos < strLen - 1; currPos++) {
        currLet = str.charAt(currPos);
        //System.out.println("Curr char: "+currLet+"  Next Char: "+str.charAt(currPos+1));
        if (currLet == str.charAt(currPos + 1)) {
            currLen++;
        } else if (currLen > maxLen) {
            maxLen = currLen;
            currLen = 1;
        } else {
            currLen = 1;
        }
    }
    if (currLen > maxLen) {
        maxLen = currLen;
    }
    

    So using

    System.out.println(longestRep("ccccccaaaabbbbb")); // 6 c's
    System.out.println(longestRep("ccbbbaaaaddaa")); // 4 a's
    System.out.println(longestRep("abcdeeefeeeeeeeefppppppp")); // 8 e's
    

    I get...

    6
    4
    8