neetcode/longest-substring-without-repeating-characters.py

40 lines
938 B
Python

#!/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
longest = -1
def count(ss: str):
c = {}
for ch in ss:
if ch in c.keys():
return 1
c[ch] = 1
return len(ss)
for f in range(0, len(s)):
for t in range(f+1, len(s)):
cs = count(s[f:t+1])
longest = cs if cs > longest else longest
longest = 1 if longest < 1 else longest
return longest
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)
print(solution("wwwwwwwwwsslpluuwekuaxt") == 7)