Given two string S and T, the task is to find the minimum length prefix from S which consists of all characters of string T. If S does not contain all characters of string T, print -1.
Input: S = “MarvoloGaunt”, T = “Tom”
The 12 length prefix “MarvoloGaunt” contains all the characters of “Tom”
Input: S = “TheWorld”, T = “Dio”
The string “TheWorld” does not contain the character ‘i’ from the string “Dio”.
The simplest approach to solve the problem is to iterate the string S and compare the frequency of each letter in both the prefix and T and return the length traversed if the required prefix is found. Otherwise, return -1.
Time Complexity: O(N2)
Auxiliary Space: O(1)
To optimize the above approach, follow the steps below:
- Store the frequencies of T in a dictionary dictCount.
- Store the number of unique letters with a count greater than 0 as nUnique.
- Iterate over [0, N], and obtain the ith index character of S as ch.
- Decrease the count of ch from dictCount if it exists. If this count goes to 0, decrease nUnique by 1.
- If nUnique reaches 0, return the length traversed till then.
- After complete traversal of S, if nUnique still exceeds 0, print -1.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Find the smallest window in a string containing all characters of another string
- Minimize length of Substrings containing at least one common Character
- Maximum length prefix of one string that occurs as subsequence in another
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Python | Get the smallest window in a string containing all characters of given pattern
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Print list items containing all characters of a given word
- Minimum length subarray containing all unique elements after Q operations
- Smallest number containing all possible N length permutations using digits 0 to D
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Minimize number of unique characters in string
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Minimize cost to empty a given string by removing characters alphabetically
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Minimize characters to be changed to make the left and right rotation of a string same
- Count of strings whose prefix match with the given string to a given length k
- Find length of longest subsequence of one string which is substring of another string
- Length of longest substring to be deleted to make a string equal to another 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.