Given two strings str and pattern, find the smallest substring in str containing all characters of pattern efficiently.
Input : str = 'geeksforgeeks' pattern = 'gks' Output : geeks Input : str = 'new string' pattern = 'rg' Output : ring
Approach #1 : Using Python enumerate()
This method uses Python
enumerate(). need[k] store how many times we need character k and missing tells how many characters are still missing. In the loop, first add the new character to the window. Then, if nothing is missing, remove as much as possible from the window start and then update the result.
Approach #2 : Using
This method make use of two defaultdicts ‘src’ and ‘dest’. A defaultdict works exactly like a normal dict, but it is initialized with a function (“default factory”) that takes no arguments. source is empty while target consist of pattern elements as keys and the count of occurrence as value. In every iteration ‘i’, we check if the ith element of str is present in target dictionary or not and update source dictionary accordingly.
Approach #3 : Dynamic approach
In this method, we use a for loop and in every iteration, say i, we find the shortest interval that ends in i and includes all letters in the pattern. This can be done by taking two data structures in account i.e. ‘rpos’ and ‘rdict’. rpos is a sorted list of positions where rightmost positions of characters of pattern in str are kept and rdict is a dictionary mapping from a character to the position. values of rdict is same as rpos.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Smallest window that contains all characters of string itself
- Find the smallest window in a string containing all characters of another string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 1
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if string follows order of characters defined by a pattern or not | Set 2
- Swap all occurrences of two characters to get lexicographically smallest string
- Length of the smallest sub-string consisting of maximum distinct characters
- Program to find the largest and smallest ASCII valued characters in a string
- Python - Remove front K characters from each string in String List
- Program to print window pattern
- Python - Key with all Characters in String
- Python | Extract only characters from given string
- Python | Extract characters except of K string
- Python - String with most unique characters
- Python - String uncommon characters
- Iterate over characters of a string in Python
- Python | Get positional characters from String
- Python - Smallest Length String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Prakash Singh 6