1
1
# 76. Minimum Window Substring
2
2
3
3
## Two Pointer with Map Solution
4
- - Runtime: O(S * T)
4
+ - Runtime: O(S * T) but O(S * (S+T)) due to string slicing
5
5
- Space: O(S)
6
6
- S = Number of characters in string S
7
7
- T = Number of unique characters in string T
@@ -21,13 +21,14 @@ If so, we can then try pruning with the left pointer all the way to the right po
21
21
22
22
You may think that the run-time for this is exponential, especially when we are checking the two dictionaries.
23
23
However, don't be mistaken, the comparison is actually a constant T run-time, it doesn't change based on S, but rather on T.
24
- There is one slight problem, python's implementation of string concatention is actually O(N).
24
+ There is one slight problem, python's implementation of string concatention is actually ** O(N)** .
25
25
When the question wants the actual substring and not a count, even using a deque will not solve this problem.
26
- So this implementation is technically O(S * (S+T)) due to python, but in other languages
26
+ So this implementation is technically ** O(S * (S+T))** due to python.
27
27
28
28
When implementing these type of two pointer questions.
29
29
I recommend to avoid using indexes as much as possible and use iterators.
30
30
It is very easy to get a one off error doing these and within a 30 minute timeframe, it is very risky.
31
+ Just talk about using indexes instead and you will be fine.
31
32
32
33
```
33
34
from collections import defaultdict
@@ -38,18 +39,18 @@ class Solution:
38
39
all_ch_counts = Counter(t)
39
40
ch_to_n_counts = defaultdict(int)
40
41
str_builder, min_substr, found = '', s, False
41
- for curr_right in s:
42
- ch_to_n_counts[curr_right ] += 1
43
- str_builder += curr_right
42
+ for right_ch in s:
43
+ ch_to_n_counts[right_ch ] += 1
44
+ str_builder += right_ch
44
45
if chars_occur_ge(ch_to_n_counts, all_ch_counts):
45
- for curr_left in str_builder:
46
+ for left_ch in str_builder:
46
47
if chars_occur_ge(ch_to_n_counts, all_ch_counts):
47
48
found = True
48
49
if len(str_builder) < len(min_substr):
49
50
min_substr = str_builder
50
51
else:
51
52
break
52
- ch_to_n_counts[curr_left ] -= 1
53
+ ch_to_n_counts[left_ch ] -= 1
53
54
str_builder = str_builder[1:]
54
55
return min_substr if found else ''
55
56
0 commit comments