查找相同字符的最长连续子序列的长度

时间:2015-03-27 02:04:40

标签: java debugging response

我试图从字符串中找到连续字符的最长重复子序列。

public int longestRep(String str) {

}

使用

调用方法时
longestRep("ccbbbaaaaddaa"); //Should return 4

到目前为止我使用的代码是;

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

但是,我收到的回复无效,不确定我做错了什么。 我是Java的新手。我刚刚编写的一些代码,可能/可能没有被使用。

2 个答案:

答案 0 :(得分:1)

尝试:

public int longestRep(String s){
    char[] c = s.toCharArray();
    int amt = 0;
    char current;
    int  max=0;

    for(int i = 0; i < c.length; i ++){
         if(current == c[i])
             amt ++;
         else{

             amt = 1;
             current = c[i];
         }
         if (max < amt) 
              max=amt;
    }

    return max;
}

这将迭代你的字符串并检查i处的字符是否与当前字符相同,如果是,它会向amt添加一个字符,否则它会将当前字符设置为char {1}}并将amt设置为1。

答案 1 :(得分:1)

boolean atBeginning = true;之后,我无法做出任何事情的头脑或尾巴,坦率地说,它似乎并不是必需的......

你的基本逻辑应该遵循......

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

您还需要对循环外的currLen is greater then maxLen执行另一项检查,以说明最后一个字符可能发生的任何更改

更像是......

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

所以使用

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

我明白了......

6
4
8