Java 查找不重复字符的最长子字符串的长度

Java 查找不重复字符的最长子字符串的长度,java,string,Java,String,所以我要解决的问题是,给定一个字符串,求出最长子字符串的长度,而不需要重复字符。我知道基于HashMap的解决方案,但如果子字符串重叠,它就会失败 public static int lengthOfLongestSubstring(String s) { Deque<Character> primary = new ArrayDeque<>(); Deque<Character> secondary = new ArrayD

所以我要解决的问题是,给定一个字符串,求出最长子字符串的长度,而不需要重复字符。我知道基于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();

    }
public static int lengthOfLongestSubstring(字符串s){
Deque primary=new ArrayDeque();
Deque secondary=新的ArrayDeque();
对于(int i=0;i
这在我做
primary=secondary
的那一行失败,否则我认为我在逻辑上做得对。 为了测试正确性,我使用字符串
dvdf

有人能帮我理解为什么这不起作用。

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

通过以下链接:

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

希望有帮助。

我想知道:

primary = secondary;
secondary.clear();
这是参考作业。您可以设置
主数据
次数据
指向相同的数据并将其清除。这是你的意图吗

那么这个呢:

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();
}
public static int lengthOfLongestSubstring(字符串s){
Deque primary=new ArrayDeque();
Deque secondary=新的ArrayDeque();
Deque longest=新阵列定义();
对于(int i=0;ilongest.size()){
最长=新阵列样式(主);
}
while(primary.peek()!=c){
secondary.offerLast(primary.poll());
}
第二,要约人第一(c);
初级=次级;
secondary=new ArrayDeque();//已更改
}否则{
一级。报价第一(c);
}
}
//在生产线的末端也要检查。
if(primary.size()>longest.size()){
最长=初级;
}
返回最长的.size();
}
输出

  • dvdf
    =>3
  • dvdfvadv
    =>4
编辑

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

编辑

记录最长的时间。

您可以尝试以下方法:

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;
    }
}
公共类longeststring{
公共静态void main(字符串[]args){
System.out.println(longestSub(“abcdefgghij”);
//打印7 abcdefg是重复字符
}
公共静态int longestSub(字符串s){
如果(s==null)
返回0;
布尔[]标志=新布尔[256];
int结果=0;
int start=0;
char[]arr=s.toCharArray();
对于(int i=0;i
/*C++程序,用于打印字符串中最大的子字符串,无需重复字符。
例如,给定字符串:-abcabbabcd
不重复字符的最大子字符串是abcd*/
#包括
使用名称空间std;
int main()
{
str序列,str1;
int max=0;
字符串finalstr;
载体str2;
cin>>str;
int len=str.length();

对于(int i=0;ii不是'dvdf`
vdf
中最长的不重复字符串,这意味着长度应该是3。这在
dvdf
上有效,但我认为即使这样也会遇到错误。不能保证当前字符串是最长的字符串。对吗?我认为我们必须维护一个列表来跟踪最长的字符串d、 我不是在寻找不同的方法或解决方案。我想知道我的逻辑有什么问题。
/*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;
}