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

发布于 2024-12-11 07:15:30 字数 1513 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风和你 2024-12-18 07:15:30

遵循下面提到的算法子字符串序列

Follow the bellow mentioned algorithm sub-string sequence

俏︾媚 2024-12-18 07:15:30

我还没有测试过这个,我已经发现并修复了一个错误,但我想我现在已经涵盖了所有基础。您在问题中没有详细说明的一个限制是存在两个长度相等的子字符串的情况。即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;
}

I haven't tested this and I found and fixed one error already, but I think I've covered all the bases now. One limitation you didn't elaborate on in your question was the case in which there are two substrings of equal, longest length. i.e. abbbcccdef, I just return the first.

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;
}
终难遇 2024-12-18 07:15:30

以下是我如何实现从字符串中查找最长的子字符串。

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

Here is how I implemented to find a longest SubString from a String.

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

Test it: http://ideone.com/JM904Y

素衣风尘叹 2024-12-18 07:15:30
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文