longest substring
This commit is contained in:
commit
f0525871c5
40
longest-substring-without-repeating-characters.py
Normal file
40
longest-substring-without-repeating-characters.py
Normal file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/python
|
||||
# yigid balaban <fyb@fybx.dev>
|
||||
# neetcode 2024
|
||||
# longest substring without repeating characters
|
||||
|
||||
def solution(s: str) -> int:
|
||||
if len(s) == 0:
|
||||
return 0
|
||||
|
||||
subs = {}
|
||||
|
||||
def count(ss: str):
|
||||
c = {}
|
||||
for ch in ss:
|
||||
c[ch] = (c[ch] if ch in c.keys() else 0) + 1
|
||||
if c[ch] > 1:
|
||||
return None
|
||||
return c
|
||||
|
||||
for f in range(0, len(s)):
|
||||
subs[s[f]] = count(s[f])
|
||||
for t in range(f+1, len(s)):
|
||||
sample = s[f:t+1]
|
||||
cs = count(sample)
|
||||
if cs:
|
||||
subs[sample] = cs
|
||||
|
||||
return max(map(lambda x: len(x), subs.keys()))
|
||||
|
||||
|
||||
print(solution("") == 0)
|
||||
print(solution("a") == 1)
|
||||
print(solution("aaa") == 1)
|
||||
print(solution("aa") == 1)
|
||||
print(solution("ababab") == 2)
|
||||
print(solution("xyzyz") == 3)
|
||||
print(solution("abcccabcxyz") == 6)
|
||||
print(solution("axyzqaxyzqdf") == 7)
|
||||
print(solution("wsslpluuwekuaxt") == 7)
|
||||
|
Loading…
x
Reference in New Issue
Block a user