LeetCode重点题系列之【3. Longest Substring Without Repeating Characters】

本文探讨了如何找出给定字符串中最长的无重复字符子串长度,提供了三种不同的算法解决方案,包括使用队列和映射的方法来维护滑动窗口内的非重复字符。

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: "bbbbb"
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.

For this question, I got two possible solutions. First, I use a Queue to maintain a sliding window, which contains only the non-duplicated string. For example, when a new element came in, I will first check whether this element already pushed in the queue. While True, I will pop the head element until there aren't any duplicated string. And I will maintain a variable to keep tracking the maximal length. That will be the final result.

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if len(s)<2:
            return len(s)
        queue=[]
        i=0
        maximal=0
        while i<len(s):
            while s[i] in queue:
                queue.pop(0)
            queue.append(s[i])
            maximal=max(maximal,len(queue))
            i+=1
        return maximal

The second solution is using a map, which records the index of every element we want to use. And if the incoming element is already used, we restart from the accordingly index. 

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        
        if len(s)<2:
            return len(s)
        dp=""
        i=0
        maximal=0
        dic={}
        while i<len(s):
            if s[i] not in dic:
                dic[s[i]]=i
                dp+=s[i]
                maximal=max(maximal,len(dp))
                i+=1
            else:
                i=dic[s[i]]+1
                dic={}
                dp=""
        return maximal

The best solution is using maps for recording index of every element, and maintain a sliding window, start at start and end at i, where in the range(start, i), there are no duplicate element。

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        dic={}
        start=0
        res=0
        for i,item in enumerate(s):
            if item not in dic:
                dic[item]=i
            else:
                res=max(res,i-start)
                start=max(start,dic[item]+1)
                dic[item]=i
        return max(res,len(s)-start)

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值