mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
00b17c4c7c
commit
7fc42c79f0
|
@ -26,12 +26,11 @@ def min_window(search_str: str, target_letters: str) -> str:
|
||||||
target_count = len(target_letters)
|
target_count = len(target_letters)
|
||||||
search_len = len(search_str)
|
search_len = len(search_str)
|
||||||
|
|
||||||
|
# Return if not possible due to string lengths.
|
||||||
#Return if not possible due to string lengths.
|
if search_len < target_count:
|
||||||
if (search_len < target_count):
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
#Build dictionary with counts for each letter in target_letters
|
# Build dictionary with counts for each letter in target_letters
|
||||||
char_dict = {}
|
char_dict = {}
|
||||||
for ch in target_letters:
|
for ch in target_letters:
|
||||||
if ch not in char_dict:
|
if ch not in char_dict:
|
||||||
|
@ -39,49 +38,48 @@ def min_window(search_str: str, target_letters: str) -> str:
|
||||||
else:
|
else:
|
||||||
char_dict[ch] += 1
|
char_dict[ch] += 1
|
||||||
|
|
||||||
#Initialize window
|
# Initialize window
|
||||||
window_start = 0
|
window_start = 0
|
||||||
window_end = 0
|
window_end = 0
|
||||||
|
|
||||||
exists = False
|
exists = False
|
||||||
min_window_len = search_len + 1
|
min_window_len = search_len + 1
|
||||||
|
|
||||||
#Start sliding window algorithm
|
# Start sliding window algorithm
|
||||||
while(window_end < search_len):
|
while window_end < search_len:
|
||||||
|
# Slide window end right until all search characters are contained
|
||||||
#Slide window end right until all search characters are contained
|
while target_count > 0 and window_end < search_len:
|
||||||
while(target_count > 0 and window_end < search_len):
|
|
||||||
cur = search_str[window_end]
|
cur = search_str[window_end]
|
||||||
if cur in char_dict:
|
if cur in char_dict:
|
||||||
char_dict[cur] -= 1
|
char_dict[cur] -= 1
|
||||||
if (char_dict[cur] >= 0):
|
if char_dict[cur] >= 0:
|
||||||
target_count -= 1
|
target_count -= 1
|
||||||
window_end += 1
|
window_end += 1
|
||||||
temp = window_end - window_start
|
temp = window_end - window_start
|
||||||
|
|
||||||
#Check if window is the smallest found so far
|
# Check if window is the smallest found so far
|
||||||
if (target_count == 0 and temp < min_window_len):
|
if target_count == 0 and temp < min_window_len:
|
||||||
min_window = [window_start,window_end]
|
min_window = [window_start, window_end]
|
||||||
exists = True
|
exists = True
|
||||||
min_window_len = temp
|
min_window_len = temp
|
||||||
|
|
||||||
#Slide window start right until a search character exits the window
|
# Slide window start right until a search character exits the window
|
||||||
while(target_count == 0 and window_start < window_end):
|
while target_count == 0 and window_start < window_end:
|
||||||
cur = search_str[window_start]
|
cur = search_str[window_start]
|
||||||
window_start += 1
|
window_start += 1
|
||||||
if cur in char_dict:
|
if cur in char_dict:
|
||||||
char_dict[cur] += 1
|
char_dict[cur] += 1
|
||||||
if (char_dict[cur] > 0):
|
if char_dict[cur] > 0:
|
||||||
break
|
break
|
||||||
temp = window_end - window_start + 1
|
temp = window_end - window_start + 1
|
||||||
|
|
||||||
#Check if window is the smallest found so far
|
# Check if window is the smallest found so far
|
||||||
if (temp < min_window_len and target_count == 0):
|
if temp < min_window_len and target_count == 0:
|
||||||
min_window = [window_start-1,window_end]
|
min_window = [window_start - 1, window_end]
|
||||||
min_window_len = temp
|
min_window_len = temp
|
||||||
target_count = 1
|
target_count = 1
|
||||||
|
|
||||||
if (exists):
|
if exists:
|
||||||
return search_str[min_window[0]:min_window[1]]
|
return search_str[min_window[0] : min_window[1]]
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
Loading…
Reference in New Issue
Block a user