From b24dd672a847fa559d8c3b401863de9ccb11297e Mon Sep 17 00:00:00 2001 From: Yigid BALABAN Date: Wed, 11 Sep 2024 18:57:27 +0300 Subject: [PATCH] t'was a mess, now less --- ...-substring-without-repeating-characters.py | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/longest-substring-without-repeating-characters.py b/longest-substring-without-repeating-characters.py index 18a1466..4c4ab87 100644 --- a/longest-substring-without-repeating-characters.py +++ b/longest-substring-without-repeating-characters.py @@ -7,25 +7,23 @@ def solution(s: str) -> int: if len(s) == 0: return 0 - subs = {} + longest = -1 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 + if ch in c.keys(): + return 1 + c[ch] = 1 + return len(ss) 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 + cs = count(s[f:t+1]) + longest = cs if cs > longest else longest + longest = 1 if longest < 1 else longest - return max(map(lambda x: len(x), subs.keys())) + return longest print(solution("") == 0) @@ -37,4 +35,5 @@ print(solution("xyzyz") == 3) print(solution("abcccabcxyz") == 6) print(solution("axyzqaxyzqdf") == 7) print(solution("wsslpluuwekuaxt") == 7) +print(solution("wwwwwwwwwsslpluuwekuaxt") == 7)