forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_Substring.py
42 lines (38 loc) · 1.23 KB
/
longest_Substring.py
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
39
40
41
42
def longest_substring_length(string):
"""Returns length of the longest substring without repetition of characters """
dct = {}
result = 0
start = 0
counter = 0
for index, val in enumerate(string):
if val not in dct:
dct[val] = index
counter += 1
else:
if dct[val] >= start:
start = dct[val] + 1
counter = index - dct[val] -1
counter += 1
dct[val] = index
result = max(result, counter)
return result
test_case = int(input('No. of test cases:'))
while test_case:
print(longest_substring_length(input()))
test_case -= 1
"""
Input format:
3 #No. of Test cases
abccddabc
bb
pwwkew
Approach:
di -> dictionary is used to keep track of duplicates.
counter-> to keep count of distinct chars since last occurence of duplicate character.
start-> start pointer from where counter starts.
If the character has its duplicate in the dictionary, we check its last occurence,
if its greater than start, then the counter becomes currentIndex - lastOccurence. Start becomes lastOccurence + 1
Complexity: Time: O(n) n-> length of string
Space: O(n)
contributed by @pradeep98
"""