MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / lengthOfLongestSubstring

Method lengthOfLongestSubstring

String/LongesSubstrings.py:28–54  ·  view source on GitHub ↗

:type s: str :rtype: int

(self, s)

Source from the content-addressed store, hash-verified

26
27class Solution(object):
28 def lengthOfLongestSubstring(self, s):
29 """
30 :type s: str
31 :rtype: int
32 """
33 if not s:
34 return 0
35
36 longestSubstringLength = 1
37
38 new = s[0]
39
40 for data in s[1:]:
41 if data not in new:
42 new += data
43 continue
44
45 # repeated.
46 if len(new) > longestSubstringLength:
47 longestSubstringLength = len(new)
48
49 new = new[new.index(data)+1:] + data
50
51 if len(new) > longestSubstringLength:
52 return len(new)
53
54 return longestSubstringLength

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected