LeetCode- longest substring of characters without repeating (sliding window method)

Given a string, you find out which does not contain a sub-length of the longest string of repeated characters.

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: Because the longest substring of characters without repetition is "abc", so its length is 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: Because the longest substring is repeated characters without "b", so that its length is 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: Because the longest sub-string is repeated characters without "wke", so its length is 3.
     Please note that your answer must be a substring of length, "pwke" is a sub-sequence, not a substring.

Source: stay button (LeetCode)
link:
https://leetcode-cn.com/problems/longest-substring-without-repeating-characters

analysis:

These three methods are broadly belonging to the sliding window method , except that the data structure of the operation as well as some different

Method 1: The character set is determined whether the presence of the substring

Wherein determining the complexity of a string of violence which the presence of the character, the need to traverse substrings, resulting in violent time complexity is O (N ^ 2), whether a character string is present in the sub-set of set determination may be employed, such character sub-string is determined whether there is a time complexity of O (1)

Time complexity: O (2 * N) = O (N), in the worst case, each character is accessed twice i and j.

Space complexity: the size of the set depends on the character string length n and the set size m, so that the space complexity O (min (n, m))

class Solution {
public:
int lengthOfLongestSubstring(string str)
{
    if(str=="")
        return 0;
    set<char> ss;
    int i=0,j=0,n=str.length();
    int ans=-1;
    while(i<n&&j<n)
    {
        if(ss.find(str[j])==ss.end())
        {
            ss.insert(str[j++]);
            ans= max (ANS, J, I); 
        } Else 
        { 
            ss.erase (str [i ++ ]); 
        } 
    } 
    Return ans; 
} 
};

Execution Time: 60 MS

Method two: The map or array index position of the last occurrence of each character is stored, so that i leapfrogging

In the S [i] to S [j] If the character is within the s [k] is repeated for S [j + 1], then, if set, i increases gradually (one per forward), but if a map the index position of the last occurrence of each character is stored, so you can skip i k + 1, belonging to the leapfrogging

Alphabet size: m

Time complexity: O (N * 2 ) = O (N)

Complexity Space: O (m)

class Solution {
public:
int lengthOfLongestSubstring(string str)
{
    if(str=="")
        return 0;
    map<char,int> mm;
    int i=0,j=0,n=str.length();
    int ans=-1;
    for(i=0,j=0;j<n;j++)
    {
        if(mm.find(str[j])!=mm.end())
        {
            i=max(i,mm[str[j]]);
        }
        ans=max(ans,j-i+1);
        mm[str[j]]=j+1;
    }
    return ans;
}
};

Execution Time: 32 MS


Method three: The array stores character index, representing the beginning of the start there is no duplication of the substring

If the position of the original characters appeared larger than the start, then start update

i-start = the length of the substring is not repeated

This method eliminates the need for using the map or set substring lookup operation whether there is a character, a direct current is determined at the last position of the character is greater than the start appeared

Time complexity: O (N)

Space complexity: O (m), m is the size of the character set

class Solution
{
public:
    int lengthOfLongestSubstring(string str)
    {
        vector<int> v(300,-1);
        int ans=0;
        int n=str.length();
        int start=-1;
        for(int i=0; i<n; i++)
        {
            if(v[str[i]]>start)
            {
                start=v[str[i]];
            }
            v[str[i]]=i;
            ans=max(ans,i-start);
        }
        return ans;
    }
};

执行时间:8 ms


滑动窗口展示的动态图片请参考:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/hua-jie-suan-fa-3-wu-zhong-fu-zi-fu-de-zui-chang-z/

Guess you like

Origin www.cnblogs.com/yinbiao/p/11271893.html