From f0525871c5721b8c9b7e9af3a2f1cd22203115c0 Mon Sep 17 00:00:00 2001 From: Yigid BALABAN Date: Wed, 11 Sep 2024 18:35:20 +0300 Subject: [PATCH] longest substring --- ...-substring-without-repeating-characters.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 longest-substring-without-repeating-characters.py diff --git a/longest-substring-without-repeating-characters.py b/longest-substring-without-repeating-characters.py new file mode 100644 index 0000000..18a1466 --- /dev/null +++ b/longest-substring-without-repeating-characters.py @@ -0,0 +1,40 @@ +#!/usr/bin/python +# yigid balaban +# 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) +