Skip to content

Commit 85e4ef1

Browse files
authored
Add files via upload
1 parent 8f0a646 commit 85e4ef1

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
def find_longest_substring(input_str):
2+
# Check the length of input_str
3+
if len(input_str) == 0:
4+
return 0
5+
6+
window_start, longest, window_length = 0, 0, 0
7+
8+
last_seen_at = {}
9+
10+
# Traverse str to find the longest substring
11+
# without repeating characters.
12+
for index, val in enumerate(input_str):
13+
# If the current element is not present in the hash map,
14+
# then store it in the hash map with the current index as the value.
15+
if val not in last_seen_at:
16+
last_seen_at[val] = index
17+
else:
18+
# If the current element is present in the hash map,
19+
# it means that this element may have appeared before.
20+
# Check if the current element occurs before or after `window_start`.
21+
if last_seen_at[val] >= window_start:
22+
window_length = index - window_start
23+
if longest < window_length:
24+
longest = window_length
25+
window_start = last_seen_at[val] + 1
26+
27+
# Update the last occurrence of
28+
# the element in the hash map
29+
last_seen_at[val] = index
30+
31+
index += 1
32+
# Update the longest substring's
33+
# length and starting index.
34+
if longest < index - window_start:
35+
longest = index - window_start
36+
37+
return longest
38+
39+
40+
# Driver code
41+
def main():
42+
string = [
43+
"abcabcbb",
44+
"pwwkew",
45+
"bbbbb",
46+
"ababababa",
47+
"",
48+
"ABCDEFGHI",
49+
"ABCDEDCBA",
50+
"AAAABBBBCCCCDDDD",
51+
]
52+
for i in range(len(string)):
53+
print(i + 1, ". \t Input String: ", string[i], sep="")
54+
print("\t Length of longest substring: ",
55+
(find_longest_substring(string[i])))
56+
print("-" * 100)
57+
58+
59+
if __name__ == "__main__":
60+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
def min_window(s, t):
2+
# empty string scenario
3+
if t == "":
4+
return ""
5+
6+
# creating the two hash maps
7+
req_count = {}
8+
window = {}
9+
10+
# populating req_count hash map
11+
for c in t:
12+
req_count[c] = 1 + req_count.get(c, 0)
13+
14+
# populating window hash map
15+
for c in t:
16+
window[c] = 0
17+
18+
# setting up the conditional variables
19+
current, required = 0, len(req_count)
20+
21+
# setting up a variable containing the result's starting and ending point
22+
# with default values and a length variable
23+
res, res_len = [-1, -1], float("infinity")
24+
25+
# setting up the sliding window pointers
26+
left = 0
27+
for right in range(len(s)):
28+
c = s[right]
29+
30+
# if the current character also occurs in t, update its frequency in window hash map
31+
if c in t:
32+
window[c] = 1 + window.get(c, 0)
33+
34+
# updating the current variable
35+
if c in req_count and window[c] == req_count[c]:
36+
current += 1
37+
38+
# adjusting the sliding window
39+
while current == required:
40+
# update our result
41+
if (right - left + 1) < res_len:
42+
res = [left, right]
43+
res_len = (right - left + 1)
44+
45+
# pop from the left of our window
46+
if s[left] in t:
47+
window[s[left]] -= 1
48+
49+
# if the popped character was among the required characters and
50+
# removing it has reduced its frequency below its frequency in t, decrement current
51+
if s[left] in req_count and window[s[left]] < req_count[s[left]]:
52+
current -= 1
53+
left += 1
54+
left, right = res
55+
56+
# return the minimum window substring
57+
return s[left:right+1] if res_len != float("infinity") else ""
58+
59+
# driver Code
60+
def main():
61+
s = ["PATTERN", "LIFE", "ABRACADABRA", "STRIKER", "DFFDFDFVD"]
62+
t = ["TN", "I", "ABC", "RK", "VDD"]
63+
for i in range(len(s)):
64+
print(i + 1, ".\ts: ", s[i], "\n\tt: ", t[i], "\n\tThe minimum substring containing ", \
65+
t[i], " is: ", min_window(s[i], t[i]), sep="")
66+
print("-" * 100)
67+
68+
if __name__ == '__main__':
69+
main()

0 commit comments

Comments
 (0)