Skip to content

Commit 04b8220

Browse files
author
Joseph Luce
authored
Update 76_minimum_window_substring.md
1 parent aecd1e8 commit 04b8220

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

leetcode/hard/76_minimum_window_substring.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 76. Minimum Window Substring
22

33
## Two Pointer with Map Solution
4-
- Runtime: O(S * T)
4+
- Runtime: O(S * T) but O(S * (S+T)) due to string slicing
55
- Space: O(S)
66
- S = Number of characters in string S
77
- 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
2121

2222
You may think that the run-time for this is exponential, especially when we are checking the two dictionaries.
2323
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)**.
2525
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.
2727

2828
When implementing these type of two pointer questions.
2929
I recommend to avoid using indexes as much as possible and use iterators.
3030
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.
3132

3233
```
3334
from collections import defaultdict
@@ -38,18 +39,18 @@ class Solution:
3839
all_ch_counts = Counter(t)
3940
ch_to_n_counts = defaultdict(int)
4041
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
4445
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:
4647
if chars_occur_ge(ch_to_n_counts, all_ch_counts):
4748
found = True
4849
if len(str_builder) < len(min_substr):
4950
min_substr = str_builder
5051
else:
5152
break
52-
ch_to_n_counts[curr_left] -= 1
53+
ch_to_n_counts[left_ch] -= 1
5354
str_builder = str_builder[1:]
5455
return min_substr if found else ''
5556

0 commit comments

Comments
 (0)