-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringWithNoRepititions.py
More file actions
62 lines (51 loc) · 2.1 KB
/
Copy pathLongestSubstringWithNoRepititions.py
File metadata and controls
62 lines (51 loc) · 2.1 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
Length of the longest substring without repeating characters
Question from GeeksforGeeks
http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/
'''
NO_OF_CHARS = 256
def longestSubStringWithOutRepetitions(string):
n = len(string)
cur_len = 1 # To store the length of current substring
max_len = 1 # To store the result
prev_index = 0 # To store the previous index
i = 0
# Initialize the visited array as -1, -1 is used to indicate
# that character has not been visited yet.
visited = [-1] * NO_OF_CHARS
# Mark first character as visited by storing the index of
# first character in visited array.
visited[ord(string[0])] = 0
# Start from the second character. First character is already
# processed (cur_len and max_len are initialized as 1, and
# visited[str[0]] is set
for i in xrange(1, n):
prev_index = visited[ord(string[i])]
# If the currentt character is not present in the already
# processed substring or it is not part of the current NRCS,
# then do cur_len++
if prev_index == -1 or (i - cur_len > prev_index):
cur_len += 1
# If the current character is present in currently considered
# NRCS, then update NRCS to start from the next character of
# previous instance.
else:
# Also, when we are changing the NRCS, we should also
# check whether length of the previous NRCS was greater
# than max_len or not.
if cur_len > max_len:
max_len = cur_len
cur_len = i - prev_index
# update the index of current character
visited[ord(string[i])] = i
# Compare the length of last NRCS with max_len and update
# max_len if needed
if cur_len > max_len:
max_len = cur_len
return max_len
# Driver program to test the above function
string = "ABDEFGABEF"
print("The input string is ", string)
length = longestSubStringWithOutRepetitions(string)
print ("The length of the longest non-repeating character" +
" substring is " + str(length))