Skip to content

Commit

Permalink
Create longest-substring.py
Browse files Browse the repository at this point in the history
  • Loading branch information
masterpol authored Aug 1, 2023
1 parent aaf80e0 commit f40ddf8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions longest-substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Problem: Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Write a function called length_of_longest_substring that takes a string s as input and returns an integer representing the
length of the longest substring without repeating characters
"""
from functools import reduce

def length_of_longest_substring(s: str) -> int:
longest = 0
current = ""

while len(s) > 0:
current = f"{current}{s[0]}" if current.find(s[0]) == -1 else s[0]

if len(current) > longest:
longest = len(current)

s = str(s[1:])

return longest


print(length_of_longest_substring("abcabcbb")) # Output: 3 ("abc" or "bca" or "cab")
print(length_of_longest_substring("bbbbb")) # Output: 1 ("b")
print(length_of_longest_substring("pwwkew")) # Output: 3 ("wke" or "kew")

0 comments on commit f40ddf8

Please sign in to comment.