mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create str-longest-non-repeating.py
This commit is contained in:
parent
3bbe283cd7
commit
e428fb1d50
38
arrays_and_strings/str-longest-non-repeating.py
Normal file
38
arrays_and_strings/str-longest-non-repeating.py
Normal file
@ -0,0 +1,38 @@
|
||||
### find the length of the longest substring without repeating characters
|
||||
|
||||
|
||||
def length_longest_substring(s: str) -> int:
|
||||
|
||||
result = ""
|
||||
this_longest_string = ""
|
||||
|
||||
i = 0
|
||||
|
||||
for c in s:
|
||||
|
||||
j = 0
|
||||
|
||||
# this loop breaks if repeated
|
||||
while j < len(this_longest_string):
|
||||
|
||||
if c == this_longest_string[j]:
|
||||
if len(this_longest_string) > len(result):
|
||||
result = this_longest_string
|
||||
this_longest_string = this_longest_string[j+1:]
|
||||
|
||||
j += 1
|
||||
|
||||
# this loop continues creating the string
|
||||
this_longest_string += c
|
||||
|
||||
|
||||
return result, this_longest_string
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
s = "abcabcbb"
|
||||
print(length_longest_substring(s))
|
||||
|
||||
s = "dvdf"
|
||||
print(length_longest_substring(s))
|
Loading…
x
Reference in New Issue
Block a user