In string manipulation problems, a frequent challenge is determining the smallest substring that contains all characters from a given string. This is a common problem in coding interviews and can test your understanding of various data structures and algorithms. In this post, we will discuss the problem in detail and provide solutions using a sliding window technique for optimal performance.
Given two strings, s
(the main string) and t
(the string containing all characters), the goal is to find the smallest substring in s
that contains all characters (including duplicates) from t
. If no such substring exists, return an empty string.
s = "ADOBECODEBANC"
, t = "ABC"
"BANC"
In the above example, the substring "BANC" contains all characters from "ABC" and is the smallest such substring.
To solve the problem effectively, we will use the sliding window technique. This approach allows us to maintain a window that expands or contracts based on whether the current substring contains all the characters from t
.
Character Count:
t
.t
are present in the current sliding window.Sliding Window:
s
.s
.t
are included in the window, attempt to contract the left pointer to find the smallest valid substring.Condition Check:
t
by comparing counters or frequency maps.Result Compilation:
s
, extract the smallest valid substring based on the recorded starting position and length.Here is a Python implementation that illustrates this approach:
def min_window(s, t): if not s or not t: return "" # Create a frequency map for characters in t dict_t = {} for char in t: dict_t[char] = dict_t.get(char, 0) + 1 required = len(dict_t) l, r = 0, 0 formed = 0 window_counts = {} min_len = float('inf') min_left = 0 while r < len(s): character = s[r] window_counts[character] = window_counts.get(character, 0) + 1 # Check if the current character fulfills the requirement if character in dict_t and window_counts[character] == dict_t[character]: formed += 1 # Try and contract the window until the point it ceases to be 'desirable' while l <= r and formed == required: character = s[l] # Update minimum length and starting index if r - l + 1 < min_len: min_len = r - l + 1 min_left = l window_counts[character] -= 1 if character in dict_t and window_counts[character] < dict_t[character]: formed -= 1 l += 1 # Keep expanding the right pointer r += 1 return "" if min_len == float('inf') else s[min_left:min_left + min_len] # Test the function s = "ADOBECODEBANC" t = "ABC" print(min_window(s, t)) # Output: "BANC"
dict_t
to keep track of how many times each character in t
should appear.l
and r
represent the start and end of the current window. We also maintain a counter formed
that increases when all necessary characters from t
are found in the current window.l
) while checking if we can still maintain the conditions needed.min_len
and min_left
to record the position.This technique provides a highly efficient solution with a time complexity of O(N + M), where N is the length of string s
and M is the length of string t
.
Using this method, you'll gain not only a strong grasp of the problem but also a toolset to tackle similar string challenges in the future. Keep practicing these techniques, and they will enhance your problem-solving skills significantly!
16/11/2024 | DSA
15/11/2024 | DSA
06/12/2024 | DSA
23/09/2024 | DSA
13/10/2024 | DSA
06/12/2024 | DSA
16/11/2024 | DSA
03/09/2024 | DSA
06/12/2024 | DSA
15/11/2024 | DSA