Given two strings string1 and string2, the task is to find the smallest substring in string1 containing all characters of string2 efficiently.
Input: string = “this is a test string”, pattern = “tist”
Output: Minimum window is “t stri”
Explanation: “t stri” contains all the characters of pattern.
Input: string = “geeksforgeeks”, pattern = “ork”
Output: Minimum window is “ksfor”
Method 1 ( Brute force solution )
1- Generate all substrings of string1 (“this is a test string”)
2- For each substring, check whether the substring contains all characters of string2 (“tist”)
3- Finally, print the smallest substring containing all characters of string2.
Method 2 ( Efficient Solution )
- First check if the length of string is less than the length of the given pattern, if yes then “no such window can exist “.
- Store the occurrence of characters of the given pattern in a hash_pat.
- Start matching the characters of pattern with the characters of string i.e. increment count if a character matches.
- Check if (count == length of pattern ) this means a window is found.
- If such window found, try to minimize it by removing extra characters from the beginning of the current window.
- Update min_length.
- Print the minimum length window.
Diagram to explain the above algorithm:
Below is the program to implement the above algorithm:
Smallest window is : t stri
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Python | Get the smallest window in a string containing all characters of given pattern
- Minimize length of prefix of string S containing all characters of another string T
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Smallest window that contains all characters of string itself
- Smallest number containing all possible N length permutations using digits 0 to D
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Print list items containing all characters of a given word
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Find smallest range containing elements from k lists
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest K-length substring containing maximum number of vowels
- Minimum number of operations to move all uppercase characters before all lower case characters
- Length of smallest substring of a given string which contains another string as subsequence
- Count characters to be shifted from the start or end of a string to obtain another string
- Swap all occurrences of two characters to get lexicographically smallest string
- Min flips of continuous characters to make all characters same in a string
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Shortest substring of a string containing all given words
- Make a string from another by deletion and rearrangement of characters