Timer is Running

Given two strings. Find the smallest window in the first string consisting of all the characters of the second string.

Example 1:

Input:
S = timetopractice
P = toc
Output: toprac
Explanation: toprac is the smallest
subset in which toc can be found.

Example 2:

Input:
S = zoomlazapzo
P = oza
Output: apzo
Explanation: To find oza in the
zoomlazapzo the smallest subset is
apzo.

Your Task:
You don't need to read input or print anything. Your task is to complete the function smallestWindow() which takes two string S and P as inputs and returns the smallest window in string S having all the characters of the string P. In case there are multiple such windows of same length, return the one with the least starting index. Return "-1" in case there's no such window present. 

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)

Constraints: 
1 <= |S|, |P| <= 1000

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Yash Kodesia
Author: Yash Kodesia 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.