# LeetCode Longest 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 subsequenceand not a substring.

```class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
start=0
max_length=0
n=len(s)
sub_str=''
for end in range(n):
if s[end] not in sub_str:
sub_str+=s[end]
else:
max_length=max(max_length,len(sub_str))
while s[start]!=s[end]:
start+=1
start+=1
sub_str=s[start:end+1]
return max(max_length,len(sub_str))```

```class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
chars = {}
begin = 0
result = 0
for i, c in enumerate(s, 1):
if (c in chars) and (chars[c] > begin):
begin = chars[c]
elif (i - begin > result):
result = i - begin
chars[c] = i
return result```

178 篇文章33 人订阅

0 条评论

## 相关文章

### leetcode222求完全二叉树节点个数

3714

Given a non-negative integer num, repeatedly add all its digits until the resul...

1995

3248

964

1161

2067

3207

3755

2096

### 数据结构 第13讲 三元组 (F、C、L/R) 序列创建二叉树

/* 输入三元组 (F、C、L/R) 序列输入一棵二叉树的诸边(其中 F 表示双亲结点的标识，C 表示孩子结点标识，L/R...

3043