- Related Questions & Answers
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to count k length substring that occurs more than once in the given string in Python
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find minimum swaps required to make given anagram in python
- Python Program to check if a substring is present in a given string.
- Does Python have a string 'contains' substring method?
- MongoDB query to find documents whose array contains a string that is a substring of a specific word
- Find if an undirected graph contains an independent set of a given size in Python
- Program to find a good string from a given string in Python
- Program to Find Out the Smallest Substring Containing a Specific String in Python
- Find the longest substring with k unique characters in a given string in Python
- Program to find longest nice substring using Python
- Program to find total number of strings, that contains one unique characters in Python
- Program to find number of sublists that contains exactly k different words in Python
- Python program to find occurrence to each character in given string

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two strings s and t, we have to find the size of a minimum substring in s that contains all the characters of t. If there is no such substring exists then return -1.

So, if the input is like s = "thegrumpywizardmakes" t = "wake", then the output will be 10, as the shortest substring that contains "wake" is "wizardmake" (length of 10).

To solve this, we will follow these steps −

counter := frequency of each character in b

start := 0

min_subs := inf

rem := count of distinct characters in b

for end in range 0 to size of a, do

current := a[end]

if current is in counter, then

counter[current] := counter[current] - 1

if counter[current] is same as 0, then

rem := rem - 1

while rem is same as 0, do

prev_char := a[start]

if prev_char is in counter, then

counter[prev_char] := counter[prev_char] + 1

if counter[prev_char] > 0, then

rem := rem + 1

min_subs := minimum of min_subs and (end - start + 1)

start := start + 1

return min_subs when min_subs is not inf otherwise -1

Let us see the following implementation to get better understanding −

class Solution: def solve(self, a, b): counter = {} for char in b: counter[char] = counter.get(char, 0) + 1 start = 0 min_subs = float("inf") rem = len(counter) for end in range(len(a)): current = a[end] if current in counter: counter[current] -= 1 if counter[current] == 0: rem -= 1 while rem == 0: prev_char = a[start] if prev_char in counter: counter[prev_char] += 1 if counter[prev_char] > 0: rem += 1 min_subs = min(min_subs, end - start + 1) start += 1 return min_subs if min_subs != float("inf") else -1 ob = Solution() s = "thegrumpywizardmakes" t = "wake" print(ob.solve(s, t))

"thegrumpywizardmakes", "wake"

2

Advertisements