3. Longest Substring Without Repeating Character [M] is not the maximum repetition substring

topic


Given a string, find the length of the longest substring without repeating characters.
Example 1:
 Input:"abcabcbb"
  Output:3
 Explanation:The answer is "abc",with the length of 3.
Example 2:
 Input:"bbbb"
 Output:1
 Explanation:The answer is "b",with the length of 1.
Example 3:
 Input:"pwwkew"
 Output:3
 Explanation:The answer is "wke",with the length of 3.
           Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Thinking


Thinking 1: sliding window method

First analysis of the meaning of problems, this problem is not seeking a repeat longest substring, we need to pay attention to two things: First, do not repeat; the second is the substring, not the sequence, which means that characters must be continuous.

Thinking 2: Optimization of the sliding window method

And mapping between ASCII characters and decimal integer table, instead of thinking a mapping between the character position in the hash table to establish the last character in the string that appears through. Since ACSII table can represent a total of 256 characters, so we can build a 256-dimensional array instead of a hash table, and each element of the array is initialized to -1. So do not look for mapping characters for each character to traverse, he updates the corresponding position in the array (according to the ASCII table will be converted to the corresponding character int), then the elements of the array to update the left edge of the window. In this way, it does not specifically establish the mapping, but according to the ASCII character table and integer mapping speed.

C++


  • Ideas 1
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        
        unordered_map<char,int> map;  //建立字符与该字符在s中最后出现的位置之间的映射
        int left = -1; // 左边界
        int result = 0; //长度
        for(int i = 0;i < s.size(); i++){
            
            if(map.count(s[i]) && map[s[i]] > left)
                left = map[s[i]];

            map[s[i]]=i;
            result = max(result, i-left);           
        }
        return result;
    }
};
  • Ideas 2
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        
        vector<int> map(256,-1);
        int left = -1;
        int result = 0;
        for(int i = 0;i < s.size(); i++){
            
            if(map[s[i]] > left)
                left = map[s[i]];

            map[s[i]]=i;
            result = max(result, i-left);      
        }
        return result;
    }
};

Python

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        
        table = { }
        left = -1
        result = 0
        for i in range(len(s)):
            if (s[i] in table) and table[s[i]] > left:
                left = table[s[i]]
            
            table[s[i]] = i
            result = max(result, i - left)
            
        return result

Guess you like

Origin www.cnblogs.com/Jessey-Ge/p/10988584.html