找到最长子字符串的长度而不重复字符

时间:2016-04-19 14:34:41

标签: java string

所以问题是我试图解决这个问题给定一个字符串,找到最长子字符串的长度而不重复字符。我知道基于HashMap的解决方案,但是在重叠子串的情况下会失败。这是我的代码。

public static int lengthOfLongestSubstring(String s) {

        Deque<Character> primary = new ArrayDeque<>();
        Deque<Character> secondary = new ArrayDeque<>();

        for (int i = 0; i < s.length() ; i++) {
            char c = s.charAt(i);
            if(primary.contains(c)){
                while(primary.peek() != c){
                    secondary.offerLast(primary.poll());
                }
                secondary.offerFirst(c);
                primary = secondary;
                secondary.clear();
            }else{
                primary.offerFirst(c);
            }
        }
        return primary.size();

    }

这在我做primary = secondary的行上失败了,否则我认为我在逻辑上做得对。 为了测试正确性我正在使用字符串dvdf 有人可以帮助我理解为什么这不起作用。

4 个答案:

答案 0 :(得分:1)

可能不是你正在寻找的确切答案。尽量避免在多线程环境中使用ArrayDeque,因为它不是线程安全的。

浏览此链接::

Find longest substring without repeating characters

这会返回一个字符串。您可以使用.length()方法并根据需要查找长度。

希望它有所帮助。

答案 1 :(得分:1)

我想知道这个:

primary = secondary;
secondary.clear();

通过引用分配。您将primarysecondary设置为指向相同的数据并清除它。这是你的意图吗?

这个怎么样:

public static int lengthOfLongestSubstring(String s) {

    Deque<Character> primary = new ArrayDeque<>();
    Deque<Character> secondary = new ArrayDeque<>();
    Deque<Character> longest = new ArrayDeque<>();

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (primary.contains(c)) {
            // Store longest
            if (primary.size() > longest.size()) {
                longest = new ArrayDeque<>(primary);
            }

            while (primary.peek() != c) {
                secondary.offerLast(primary.poll());
            }
            secondary.offerFirst(c);
            primary = secondary;
            secondary = new ArrayDeque<>();  // Altered
        } else {
            primary.offerFirst(c);
        }
    }

    // Also check at end of line.
    if (primary.size() > longest.size()) {
        longest = primary;
    }

    return longest.size();
}

输出

  • dvdf =&gt; 3
  • dvdfvadv =&gt; 4

修改

你的逻辑是正确的。我只是改了一行。

修改

追踪最长时间。

答案 2 :(得分:0)

你可以试试这个:

public class LongestSubstring {
    public static void main(String [] args){
        System.out.println(longestSub("abcdefgghij"));
//prints 7 abcdefg g is repeated character
    }
    public static int longestSub(String s) {
        if(s==null)
            return 0;
        boolean[] flag = new boolean[256];

        int result = 0;
        int start = 0;
        char[] arr = s.toCharArray();

        for (int i = 0; i < arr.length; i++) {
            char current = arr[i];
            if (flag[current]) {
                result = Math.max(result, i - start);
                // the loop update the new start point and reset flag array

                for (int k = start; k < i; k++) {
                    if (arr[k] == current) {
                        start = k + 1;
                        break;
                    }
                    flag[arr[k]] = false;
                }
            } else {
                flag[current] = true;
            }
        }

        result = Math.max(arr.length - start, result);

        return result;
    }
}

答案 3 :(得分:0)

/*C++ program to print the largest substring in a string without repetation of character.
eg. given string :- abcabbabcd
largest substring possible without repetition of character is abcd.*/

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str,str1;
    int max =0;
    string finalstr;
    vector<string> str2;
    cin>>str;
    int len = str.length();
    for(int i=0;i<len;i++)
    {
        if(str1.find(str[i]) != std::string::npos)
        {
            str2.push_back(str1);
            char p = str[i];
            str1 = "";
            i--;
            while(p!=str[i])
                i--;
        }
        else
            str1.append(str,i,1);
    }
    str2.push_back(str1);

    for(int i=0;i<str2.size();i++)
    {
        if(max<str2[i].length()){
            max = str2[i].length();
            finalstr  = str2[i];
        }
    }
    cout<<finalstr<<endl;
    cout<<finalstr.length()<<endl;
}