@@ -26,62 +26,60 @@ def min_window(search_str: str, target_letters: str) -> str:
26
26
target_count = len (target_letters )
27
27
search_len = len (search_str )
28
28
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 :
32
31
return ""
33
32
34
- #Build dictionary with counts for each letter in target_letters
33
+ # Build dictionary with counts for each letter in target_letters
35
34
char_dict = {}
36
35
for ch in target_letters :
37
36
if ch not in char_dict :
38
37
char_dict [ch ] = 1
39
38
else :
40
39
char_dict [ch ] += 1
41
40
42
- #Initialize window
41
+ # Initialize window
43
42
window_start = 0
44
43
window_end = 0
45
44
46
45
exists = False
47
46
min_window_len = search_len + 1
48
47
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 :
54
52
cur = search_str [window_end ]
55
53
if cur in char_dict :
56
54
char_dict [cur ] -= 1
57
- if ( char_dict [cur ] >= 0 ) :
55
+ if char_dict [cur ] >= 0 :
58
56
target_count -= 1
59
57
window_end += 1
60
58
temp = window_end - window_start
61
59
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 ]
65
63
exists = True
66
64
min_window_len = temp
67
65
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 :
70
68
cur = search_str [window_start ]
71
69
window_start += 1
72
70
if cur in char_dict :
73
71
char_dict [cur ] += 1
74
- if ( char_dict [cur ] > 0 ) :
72
+ if char_dict [cur ] > 0 :
75
73
break
76
74
temp = window_end - window_start + 1
77
75
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 ]
81
79
min_window_len = temp
82
80
target_count = 1
83
81
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 ]]
86
84
else :
87
85
return ""
0 commit comments