# Leetcode: Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

Similar Problems:

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.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

```## Blog link: https://code.dennyzhang.com/longest-substring-without-repeating-characters
## Basic Ideas: two pointer: slow and fast
##           If duplicate, the fast pointer don't need to turn back.
## Complexity Time O(n), Space O(1)
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
length = len(s)
if length <= 1: return length
ch_set = set()
max_len, slow, fast = 0, 0, 1
m = {}
m[s[slow]] = slow
while fast < length:
if s[fast] not in m:
m[s[fast]] = fast
fast += 1
else:
max_len = max(max_len, len(m))
next_slow = m[s[fast]] + 1
for k in xrange(slow, next_slow):
del m[s[k]]
m[s[fast]] = fast
slow = next_slow
fast += 1
return max(max_len, len(m))

# s = Solution()
# print s.lengthOfLongestSubstring('ruowzgiooobpple')
# print s.lengthOfLongestSubstring('bbbbb') # 1
# print s.lengthOfLongestSubstring('pwwkew') # 3
```

Share It, If You Like It.