


You are given two strings ‘A’ and ‘B’. Your task is to return a substring ‘S’ of ‘A’ such that the following conditions hold true :
• You can make ‘B’ from ‘S’ by removing some characters and rearranging some characters zero or more times.
• Length of ‘S’ must be as minimum as possible.
Note :
Testcases are generated such that a substring always exists and is unique.
Example :
A = ninjas, B = sin
All possible substrings with which 'B' can be created are
"ninjas", "injas".
Hence the substring with minimum length is "injas".
Input Format :
The first line contains two space-separated strings ‘A’ and ‘B’.
Output Format :
Print the substring with minimum length.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
fight it
ight
Given A = “fight” and B = “it”
Consider the substring “ight” of A.
We can remove g and h from it to get “it”.
We can also get "it" from "fight" but it is not the substring with minimum length.
coding cin
codin
1 <= |A| = |B| <= 3000
Both A, B contain only lowercase English letters.
Where |A| and |B| are the length of strings.
Time Limit: 1 sec
Check all possible substrings.
The idea here is to generate all possible substrings of “A” and check if it contains all characters of “B” or not.
O( ( |A| ^ 2 ) + |A| + |B| ), where |A| is the length of the given string ‘A’ and |B| is the length of the given string ‘B’.
We are running two loops to generate all substring that takes O(|A| ^ 2) time. Also checking the validity of a substring takes O(26) time as both strings can contain at most 26 characters. O(26) is basically O(1). Also, our ‘count’ map takes O( |A| ) time and ‘countCurrent’ map takes O( |B| ) time to store frequencies of string ‘A’ and ‘B’ respectively.
So overall time complexity = O( ( |A| ^ 2 ) + |A| + |B| ).
O(1).
We are using two maps that take O(26) space as there can be a maximum of 26 characters in the map. O(26) is basically O(1).