首页 > 其他 > 详细

leetcode(3): Longest Substring Without Repeating Characters

时间:2016-01-26 18:36:18      阅读:257      评论:0      收藏:0      [点我收藏+]

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

Analysis

If a repeat char is found, there is no need to search chars before the previous repeat char, so start to search from char after the previous repeat char.

Codes

#!/usr/bin/python

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        d = dict()
        maxLength = 0
        index = 0
        
        while index < len(s):
            if s[index] in d:
                if maxLength < len(d):
                    maxLength = len(d)
                index = d[s[index]] + 1
                d.clear()
            else:
                d[s[index]] = index
                index += 1
                
        if maxLength < len(d):
            maxLength = len(d)
            
        return maxLength

leetcode(3): Longest Substring Without Repeating Characters

原文:http://www.cnblogs.com/luckysimple/p/5161026.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!