String
1 min readApr 16, 2020
3. Longest Substring Without Repeating Characters
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
used = {}
start = 0
res = 0
for i,c in enumerate(s):
if c in used and used[c]>=start:
start = used[c]+1
else:
res=max(res, i-start+1)
used[c] = i
return res
class Solution:
def partitionLabels(self, S: str) -> List[int]:
if not S:return []
d = {}
for i,c in enumerate(S):
d[c] = i
res, this_res = [], 0
seen = set()
for i, c in enumerate(S):
this_res+=1
if i==d[c]:
seen.discard(c)
if not seen:
res.append(this_res)
this_res = 0
else:
seen.add(c)
return res