Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Smallest Window of All Characters

Hard Acceptance 50.00% Points 40.00

Given two strings s1 and s2. Find the smallest window in the string s1 consisting of all the characters(including duplicates) of the string s2. return empty string in case no such window is present.

If there are multiple such windows of the same length, return the one with the least starting index.

Note: All characters are in lowercase letters.

Examples
Example 1

Example 1

Input: s1 = "timetopractice", s2 = "toc"

Output: "toprac"

Explanation: "toprac" is the smallest substring in which "toc" can be found.

Example 2

Example 2

Input: s1 = "zoomlazapzo", s2 = "oza"

Output: "apzo"

Explanation: "apzo" is the smallest substring in which "oza" can be found.

Example 3

Example 3

Input: s1 = "zoom", s2 = "zooe"

Output: ""

Explanation: No window is present containing all characters of s2.

Hints
Hint 1
NA
Constraints
  • 1 = |s1|, |s2| = 10^5
Companies
Amazon Microsoft Google Atlassian Flipkart + 1 more
Topics
String Dynamic Programming
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions