从Java中传递的字符串中获取最长的子字符串

时间:2011-10-22 23:43:46

标签: java arrays string if-statement for-loop

我想将一个字符串传递给一个方法,例如“abbcccdef”并希望它返回最长的子串。在这种情况下,它将是“ccc”。有人请帮我解决这个问题的代码。我想要一些基本的东西,让初学者很容易理解。这是我到目前为止所做的,但似乎没有效果:

非常感谢

public String getLongestSubstring(String s) {

        int [] length = new int [s.length()];
        String longestString = "";
        if (s.length() > 0) {
            char c = s.charAt(0);
            for (int i=0;i<s.length();i++) {
                for (int j=0;j<s.length();j++) {
                    if (c==s.charAt(j)) {
                        length [i]++;
                    } else {
                        c = s.charAt(j);
                        i++;
                    }
                }

            }
            return longestString;
        }
        else
        return null;

    }

4 个答案:

答案 0 :(得分:1)

我没有对此进行测试,我发现并修复了一个错误,但我认为我现在已经覆盖了所有基础。你在问题中没有详细说明的一个限制是有两个相等,最长的子串。即abbbcccdef,我只返回第一个。

public String getLongestSubstring(String s)
{
    if (s.length() == 0) return null;

    //We need some data to start with.
    char currentChar = s.charAt(0);
    String longestString = "" + currentChar;
    String runningString = "" + currentChar;
    int currentLongestLength = 1;

    for (int i = 1; i < s.length(); i++)
    {
        //Check the current char, is it the same?
        if (s.charAt(i) == currentChar)
        {
            runningString = runningString + currentChar;
            //Have we beaten our previous record.
            if (runningString.length() > longestString.length())
            {
                //Increase the record.
                currentLongestLength++;
                longestString = runningString;
            }
        }
        else
        {
            runningString = "" + s.charAt(i); 
        }
    }
    return longestString;
}

答案 1 :(得分:1)

按照下面提到的算法sub-string sequence

答案 2 :(得分:1)

以下是我如何实现从String中找到最长的SubString。

public class LongestString {    

    public static void main (String[] args) throws java.lang.Exception {
        System.out.println("Longest Substring is " + getLongestSubstring("abbcccdf"));
    }

    public static String getLongestSubstring(String s) {
            int length = 1;

        String longestString = "";

        for (int i = 0; i < s.length(); i++) {
            StringBuilder str = new StringBuilder();
            str.append(s.charAt(i));

            for (int j = i + 1; j < s.length(); j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    str.append(s.charAt(j));
                } else {
                    break;
                }
            }
            if (length < str.length()) {
                length = str.length();
                longestString = str.toString();
            }

        }
        return longestString;
    }
}

测试一下: http://ideone.com/JM904Y

答案 3 :(得分:0)

public String getLongestSubString(String source) {

    StringBuilder longestStringBuilder = new StringBuilder(source.length());
    String longestString = "";

    for(int i=0; i<source.length(); i++) {

        if(longestStringBuilder.toString().contains(String.valueOf(source.charAt(i)))) {

            if(longestString.length() < longestStringBuilder.toString().length()) {
                longestString = longestStringBuilder.toString();
            }

            longestStringBuilder.setLength(0);
        }

        longestStringBuilder.append(source.charAt(i));

    }

    if(longestString.length() < longestStringBuilder.toString().length()) {
        longestString = longestStringBuilder.toString();
    }

    return longestString;
}