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**.

**Examples:**

Input:S = “MarvoloGaunt”, T = “Tom”Output:12Explanation:

The 12 length prefix “MarvoloGaunt” contains all the characters of “Tom”

Input:S = “TheWorld”, T = “Dio”Output:-1Explanation:

The string “TheWorld” does not contain the character ‘i’ from the string “Dio”.

**Naive Approach: **

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(N^{2}) **Auxiliary Space:** O(1)

**Efficient Approach: **

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**i**character of^{th}index**S**as**ch**. - Decrease the count of
**ch**fromif it exists. If this count goes to 0, decrease**dictCount**by 1.**nUnique** - If
reaches 0, return the length traversed till then.**nUnique** - After complete traversal of
**S**, ifstill exceeds 0, print**nUnique****-1**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `getPrefixLength(string srcStr, ` ` ` `string targetStr)` `{` ` ` ` ` `// Base Case - if T is empty, ` ` ` `// it matches 0 length prefix ` ` ` `if` `(targetStr.length() == 0) ` ` ` `return` `0;` ` ` ` ` `// Convert strings to lower ` ` ` `// case for uniformity ` ` ` `transform(srcStr.begin(), ` ` ` `srcStr.end(), ` ` ` `srcStr.begin(), ::` `tolower` `);` ` ` `transform(targetStr.begin(), ` ` ` `targetStr.end(), ` ` ` `targetStr.begin(), ::` `tolower` `); ` ` ` ` ` `map<` `char` `, ` `int` `> dictCount;` ` ` `int` `nUnique = 0;` ` ` ` ` `// Update dictCount to the ` ` ` `// letter count of T ` ` ` `for` `(` `char` `ch: targetStr)` ` ` `{` ` ` ` ` `// If new character is found, ` ` ` `// initialize its entry, ` ` ` `// and increase nUnique ` ` ` `if` `(dictCount.find(ch) == ` ` ` `dictCount.end())` ` ` `{` ` ` `nUnique += 1;` ` ` `dictCount[ch] = 0;` ` ` `}` ` ` ` ` `// Increase count of ch ` ` ` `dictCount[ch] += 1;` ` ` `}` ` ` ` ` `// Iterate from 0 to N ` ` ` `for` `(` `int` `i = 0; i < srcStr.length(); i++)` ` ` `{` ` ` ` ` `// i-th character ` ` ` `char` `ch = srcStr[i]; ` ` ` ` ` `// Skip if ch not in targetStr ` ` ` `if` `(dictCount.find(ch) ==` ` ` `dictCount.end()) ` ` ` `continue` `;` ` ` ` ` `// Decrease Count ` ` ` `dictCount[ch] -= 1;` ` ` ` ` `// If the count of ch reaches 0, ` ` ` `// we do not need more ch, ` ` ` `// and can decrease nUnique ` ` ` `if` `(dictCount[ch] == 0) ` ` ` `nUnique -= 1;` ` ` ` ` `// If nUnique reaches 0, ` ` ` `// we have found required prefix ` ` ` `if` `(nUnique == 0)` ` ` `return` `(i + 1); ` ` ` `}` ` ` ` ` `// Otherwise ` ` ` `return` `-1;` `}` `// Driver code ` `int` `main()` `{` ` ` `string S = ` `"MarvoloGaunt"` `;` ` ` `string T = ` `"Tom"` `;` ` ` ` ` `cout << getPrefixLength(S, T); ` ` ` `return` `0;` `}` `// This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `def` `getPrefixLength(srcStr, targetStr):` ` ` `# Base Case - if T is empty,` ` ` `# it matches 0 length prefix` ` ` `if` `(` `len` `(targetStr) ` `=` `=` `0` `):` ` ` `return` `0` ` ` `# Convert strings to lower` ` ` `# case for uniformity` ` ` `srcStr ` `=` `srcStr.lower()` ` ` `targetStr ` `=` `targetStr.lower()` ` ` `dictCount ` `=` `dict` `([])` ` ` `nUnique ` `=` `0` ` ` `# Update dictCount to the` ` ` `# letter count of T` ` ` `for` `ch ` `in` `targetStr:` ` ` `# If new character is found,` ` ` `# initialize its entry,` ` ` `# and increase nUnique` ` ` `if` `(ch ` `not` `in` `dictCount):` ` ` `nUnique ` `+` `=` `1` ` ` `dictCount[ch] ` `=` `0` ` ` `# Increase count of ch` ` ` `dictCount[ch] ` `+` `=` `1` ` ` `# Iterate from 0 to N` ` ` `for` `i ` `in` `range` `(` `len` `(srcStr)):` ` ` `# i-th character` ` ` `ch ` `=` `srcStr[i]` ` ` `# Skip if ch not in targetStr` ` ` `if` `(ch ` `not` `in` `dictCount):` ` ` `continue` ` ` `# Decrease Count` ` ` `dictCount[ch] ` `-` `=` `1` ` ` `# If the count of ch reaches 0,` ` ` `# we do not need more ch,` ` ` `# and can decrease nUnique` ` ` `if` `(dictCount[ch] ` `=` `=` `0` `):` ` ` `nUnique ` `-` `=` `1` ` ` `# If nUnique reaches 0,` ` ` `# we have found required prefix` ` ` `if` `(nUnique ` `=` `=` `0` `):` ` ` `return` `(i ` `+` `1` `)` ` ` `# Otherwise` ` ` `return` `-` `1` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `S ` `=` `"MarvoloGaunt"` ` ` `T ` `=` `"Tom"` ` ` `print` `(getPrefixLength(S, T))` |

*chevron_right*

*filter_none*

**Output:**

12

**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.

## Recommended Posts:

- 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
- Minimize length of string by replacing K pairs of distinct adjacent characters
- 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
- Minimize cost to convert all characters of a binary string to 0s
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Minimum length subarray containing all unique elements after Q operations
- Smallest number containing all possible N length permutations using digits 0 to D
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2
- 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
- Count characters to be shifted from the start or end of a string to obtain another string
- Count of strings whose prefix match with the given string to a given length k
- Minimize number of unique characters in string
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.