leetcode algorithm3 Substring Without Repeating Characters

来源:互联网 发布:linux实现nat网关功能 编辑:程序博客网 时间:2024/05/18 13:27

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 maxinstance = Solution()str = "tmmzuxt"print(instance.lengthOfLongestSubstring(str))print(instance.lengthOfLongestSubstringBetter(str))str = "aab"print(instance.lengthOfLongestSubstring(str))print(instance.lengthOfLongestSubstringBetter(str))
阅读全文
0 0