Created
October 28, 2020 11:55
-
-
Save wildonion/8d7e309a970f7e6614ab2309c9d35ebf to your computer and use it in GitHub Desktop.
longest substring without repeating characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# longest substring without repeating characters | |
class lols: | |
def __init__(self, string : str): | |
self.string = string | |
def solve(self) -> int: | |
if len(self.string) == 0: | |
return 0 | |
if len({self.string[i] : i for i in range(len(self.string))}) == 1: | |
return 1 | |
sub_s = [] | |
ls = self.string[0] | |
last_char = self.string[0] | |
for c in range(1, len(self.string)): | |
if self.string[c] != last_char: | |
ls+=self.string[c] | |
elif self.string[c] == last_char: | |
ls = self.string[c] | |
if len(ls) > 1: | |
sub_s.append(ls) | |
last_char = self.string[c] | |
last_sub = sub_s[len(sub_s)-1] | |
dict_sub_s = {last_sub[i] : i for i in range(len(last_sub))} | |
return len(dict_sub_s) | |
user_input = input("Enter String >>> ") | |
assert 0 <= len(user_input) <= 5 * 104 | |
p = lols(user_input) | |
n_sub_str = p.solve() | |
print(n_sub_str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment