当前位置:网站首页>Topic24——3. Longest substring without duplicate characters

Topic24——3. Longest substring without duplicate characters

2022-06-09 05:44:00 _ Cabbage_

subject : Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .

 Example  1:
 Input : s = "abcabcbb"
 Output : 3 
 explain :  Because the longest substring without repeating characters is  "abc", So its length is  3.

 Example  2:
 Input : s = "bbbbb"
 Output : 1
 explain :  Because the longest substring without repeating characters is  "b", So its length is  1.

 Example  3:
 Input : s = "pwwkew"
 Output : 3
 explain :  Because the longest substring without repeating characters is  "wke", So its length is  3.
      Please note that , Your answer must be   Substring   The length of ,"pwke"  Is a subsequence , Not substring .

Tips :
0 <= s.length <= 5 * 104
s By the English letters 、 Numbers 、 Symbols and spaces

class Solution {
    
    public int lengthOfLongestSubstring(String s) {
    
        if(s.length() == 0)
            return 0;
        int[] memo = new int[s.length()];
        Arrays.fill(memo, 1);
        int i = 1;
        int max = 1;
        while(i < s.length()) {
    
            for(int j = i - 1; j > i - memo[i - 1] - 1; j--) {
    
                if(s.charAt(j) != s.charAt(i)) {
    
                    memo[i] = memo[i] + 1;
                } else {
    
                    break;
                }
            }
            max = Math.max(max, memo[i]);
            i++;           
        }
        return max;
    }
}
原网站

版权声明
本文为[_ Cabbage_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090537599349.html