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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Find the smallest window in a string containing all characters of another string
- Smallest window that contains all characters of string itself
- Minimize length of prefix of string S containing all characters of another string T
- Swap all occurrences of two characters to get lexicographically smallest string
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Get all rows in a Pandas DataFrame containing given substring
- Print list items containing all characters of a given word
- Smallest number containing all possible N length permutations using digits 0 to D
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Shortest substring of a string containing all given words
- Python - Remove words containing list characters
- PyQtGraph – Getting Window Flags of Plot Window
- PyQtGraph – Setting Window Flag to Plot Window
- String containing first letter of every word in a given string with spaces
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest K-length substring containing maximum number of vowels
- Python - Get the indices of Uppercase characters in given string
- Minimum number of operations to move all uppercase characters before all lower case characters
- Min flips of continuous characters to make all characters same in a 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 firstname.lastname@example.org. 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