-
Notifications
You must be signed in to change notification settings - Fork 72
Description
Given two strings s and p. Find the smallest substring in s consisting of all the characters (including duplicates) of the string p. Return empty string in case no such substring is present.
If there are multiple such substring of the same length found, return the one with the least starting index.
Enhancement / Feature Request (if applicable)
Smallest Window Substring Search
Why It's Useful
Finding the smallest substring of a given string s that contains all the characters of another string p is valuable because:
Pattern Matching: Useful in text searching applications like search engines, plagiarism detection, or DNA sequence analysis.
Efficient Resource Utilization: Instead of scanning the entire string repeatedly, the sliding window technique provides an optimized solution.
Algorithm Practice: Improves understanding of sliding window, frequency counting, and string manipulation techniques.
How It Should Work
Character Frequency Tracking:
Maintain two frequency arrays (or maps): one for the characters required from string p and one for the current window in s.
Sliding Window Expansion:
Start with two pointers l and r.
Expand the right pointer (r) to include characters until all characters of p are covered.
Window Shrinking:
Move the left pointer (l) to shrink the window and check if it still contains all required characters.
Keep track of the minimum length window during this process.
Result Extraction:
If no valid window is found, return an empty string.
Otherwise, return the substring from the best window found.