Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c64f65f

Browse files
committedOct 17, 2024·
2 parents 3418c80 + 7fc42c7 commit c64f65f

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed
 

‎strings/min_window_substring.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,60 @@ def min_window(search_str: str, target_letters: str) -> str:
2626
target_count = len(target_letters)
2727
search_len = len(search_str)
2828

29-
30-
#Return if not possible due to string lengths.
31-
if (search_len < target_count):
29+
# Return if not possible due to string lengths.
30+
if search_len < target_count:
3231
return ""
3332

34-
#Build dictionary with counts for each letter in target_letters
33+
# Build dictionary with counts for each letter in target_letters
3534
char_dict = {}
3635
for ch in target_letters:
3736
if ch not in char_dict:
3837
char_dict[ch] = 1
3938
else:
4039
char_dict[ch] += 1
4140

42-
#Initialize window
41+
# Initialize window
4342
window_start = 0
4443
window_end = 0
4544

4645
exists = False
4746
min_window_len = search_len + 1
4847

49-
#Start sliding window algorithm
50-
while(window_end < search_len):
51-
52-
#Slide window end right until all search characters are contained
53-
while(target_count > 0 and window_end < search_len):
48+
# Start sliding window algorithm
49+
while window_end < search_len:
50+
# Slide window end right until all search characters are contained
51+
while target_count > 0 and window_end < search_len:
5452
cur = search_str[window_end]
5553
if cur in char_dict:
5654
char_dict[cur] -= 1
57-
if (char_dict[cur] >= 0):
55+
if char_dict[cur] >= 0:
5856
target_count -= 1
5957
window_end += 1
6058
temp = window_end - window_start
6159

62-
#Check if window is the smallest found so far
63-
if (target_count == 0 and temp < min_window_len):
64-
min_window = [window_start,window_end]
60+
# Check if window is the smallest found so far
61+
if target_count == 0 and temp < min_window_len:
62+
min_window = [window_start, window_end]
6563
exists = True
6664
min_window_len = temp
6765

68-
#Slide window start right until a search character exits the window
69-
while(target_count == 0 and window_start < window_end):
66+
# Slide window start right until a search character exits the window
67+
while target_count == 0 and window_start < window_end:
7068
cur = search_str[window_start]
7169
window_start += 1
7270
if cur in char_dict:
7371
char_dict[cur] += 1
74-
if (char_dict[cur] > 0):
72+
if char_dict[cur] > 0:
7573
break
7674
temp = window_end - window_start + 1
7775

78-
#Check if window is the smallest found so far
79-
if (temp < min_window_len and target_count == 0):
80-
min_window = [window_start-1,window_end]
76+
# Check if window is the smallest found so far
77+
if temp < min_window_len and target_count == 0:
78+
min_window = [window_start - 1, window_end]
8179
min_window_len = temp
8280
target_count = 1
8381

84-
if (exists):
85-
return search_str[min_window[0]:min_window[1]]
82+
if exists:
83+
return search_str[min_window[0] : min_window[1]]
8684
else:
8785
return ""

0 commit comments

Comments
 (0)
Please sign in to comment.