From 7fc42c79f02ecddd5f3dd9a6e0fc54afbd7e2cbd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Oct 2024 21:20:00 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/min_window_substring.py | 54 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/strings/min_window_substring.py b/strings/min_window_substring.py index 9aa85cd2d..c2bdf58ef 100644 --- a/strings/min_window_substring.py +++ b/strings/min_window_substring.py @@ -14,24 +14,23 @@ def min_window(search_str: str, target_letters: str) -> str: in the window, and shifting the start of the window right until the window no longer contains every target character. - Time complexity: O(target_count + search_len) -> - The algorithm checks a dictionary at most twice for each character + Time complexity: O(target_count + search_len) -> + The algorithm checks a dictionary at most twice for each character in search_str. - Space complexity: O(search_len) -> - The primary contributer to additional space is the building of a + Space complexity: O(search_len) -> + The primary contributer to additional space is the building of a dictionary using the search string. """ - + target_count = len(target_letters) search_len = len(search_str) - - #Return if not possible due to string lengths. - if (search_len < target_count): + # Return if not possible due to string lengths. + if search_len < target_count: return "" - #Build dictionary with counts for each letter in target_letters + # Build dictionary with counts for each letter in target_letters char_dict = {} for ch in target_letters: if ch not in char_dict: @@ -39,49 +38,48 @@ def min_window(search_str: str, target_letters: str) -> str: else: char_dict[ch] += 1 - #Initialize window + # Initialize window window_start = 0 window_end = 0 exists = False min_window_len = search_len + 1 - #Start sliding window algorithm - while(window_end < search_len): - - #Slide window end right until all search characters are contained - while(target_count > 0 and window_end < search_len): + # Start sliding window algorithm + while window_end < search_len: + # Slide window end right until all search characters are contained + while target_count > 0 and window_end < search_len: cur = search_str[window_end] if cur in char_dict: char_dict[cur] -= 1 - if (char_dict[cur] >= 0): + if char_dict[cur] >= 0: target_count -= 1 window_end += 1 temp = window_end - window_start - #Check if window is the smallest found so far - if (target_count == 0 and temp < min_window_len): - min_window = [window_start,window_end] + # Check if window is the smallest found so far + if target_count == 0 and temp < min_window_len: + min_window = [window_start, window_end] exists = True min_window_len = temp - #Slide window start right until a search character exits the window - while(target_count == 0 and window_start < window_end): + # Slide window start right until a search character exits the window + while target_count == 0 and window_start < window_end: cur = search_str[window_start] window_start += 1 if cur in char_dict: char_dict[cur] += 1 - if (char_dict[cur] > 0): + if char_dict[cur] > 0: break temp = window_end - window_start + 1 - #Check if window is the smallest found so far - if (temp < min_window_len and target_count == 0): - min_window = [window_start-1,window_end] + # Check if window is the smallest found so far + if temp < min_window_len and target_count == 0: + min_window = [window_start - 1, window_end] min_window_len = temp target_count = 1 - if (exists): - return search_str[min_window[0]:min_window[1]] + if exists: + return search_str[min_window[0] : min_window[1]] else: - return "" \ No newline at end of file + return ""