leetcode algorithm3 Substring Without Repeating Characters

leetcode algorithm3 Substring Without Repeating Characters

原题链接

问题

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, 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.

思路

先说蛮力方法。找一个buffer,按扫描顺序依次把字符串里的元素加入buffer,如果下一个加入的元素已经在buffer里了,则缩短buffer,类似KMP那样,只不过我们只用缩短一次,因为我们一直要保证buffer里不包含重复元素。缩短的办法就是把出现重复元素的地方及其前面部分扔掉。再加上下一个字符串里面的元素。同时维护一个max变量。

分析下时间复杂度。扫描一遍字符串作为外层循环代价O(n)。每次向buffer里添加元素的时候要检查buffer里是否存在该元素。如果是线性检查,则为O(n),如果使用Hash实现,则为O(1),如果使用有序结构,则为O(logn)。由于题目不要求有序返回找到的最大不重复字符串,所以我们不需要维护顺序。这里选择hashmap来作为buffer比较好。以元素作为键,index作为值。

当然也可以用set来维护这个滑动窗口,但是每次remove掉元素的时候就只能一个一个从前面remove掉再看重复元素是否还在。

代码

python实现,这里给出了buffer用数组和用map的实现方式

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        current = ""
        max = 0
        for i in range(len(s)): # range start from 0
            # if there is a existing character
            if s[i] in current:
                # we cut a part of the current to continue
                index = current.find(s[i])

                # handle the boundary value
                if index+1 < len(current):
                    current = current[index+1:]
                    current += s[i]
                else:
                    current = s[i]
            # if the character not in there, just add it to the buffer
            else:
                current += s[i]
                if len(current) > max:
                    max = len(current)
        return max

    def lengthOfLongestSubstringBetter(self, s):
        """
        :type s: str
        :rtype: int
        """
        current = {}
        left_boundary = 0
        max = 0
        for i in range(len(s)): # range start from 0
            #if current.has_key(s[i]):
            # the above function is for python 2.X, the latter is for python 3.X
            if current.__contains__(s[i]):
                # we update the length
                index = current[s[i]]
                if left_boundary <= index:
                    left_boundary = index + 1

            current[s[i]] = i
            length = i - left_boundary + 1
            if length > max:
                max = length

        return max

instance = Solution()
str = "tmmzuxt"
print(instance.lengthOfLongestSubstring(str))
print(instance.lengthOfLongestSubstringBetter(str))
str = "aab"
print(instance.lengthOfLongestSubstring(str))
print(instance.lengthOfLongestSubstringBetter(str))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值