-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_3.py
More file actions
38 lines (30 loc) · 1.31 KB
/
question_3.py
File metadata and controls
38 lines (30 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
''' Longest Substring Without Repeating Characters:
Given a string s, find the length of the longest
substring without repeating characters.'''
''' Personal Note:
A substring is categorized as slices of a string, therefore,
if s = {wkjwkwwhjeuw}, the longest substring is: {whjeu} which is 5 characters long
'''
def lengthOfLongestSubstring(s):
seen = {} #create a dictionary
max = 0 #set the max to 0
count = 0 #initialize an accumulator
for i in range(len(s)):
if s[i] not in seen: #inputs new characters into the dictionary with their index
seen[s[i]] = i
count += 1 #increase the accumulator
if count > max: #checks if max is less than the accumulator, if it is then perform a swap
max = count
else:
seen = {} #resets the dictionary
seen[s[i]] = i #enters the new character into the dictionary with its index
count = 1 #resets the accumulator to 1 to represent the new character added
return max
def main():
s = input("Enter a string: ")
max = lengthOfLongestSubstring(s)
if max == len(s):
print('No repeat characters in the string')
else:
print(f'The longest substring is {max} characters long')
main()