Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Smallest Window of All Characters

Hard Acceptance 50% Points 40

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.

Constraints
  • 1 = |s1|, |s2| = 10^5
Topics
String Dynamic Programming
Companies
Amazon Microsoft Google Atlassian Flipkart Media.net
Added 18 Aug 2025 50% acceptance
Ln 1, Col 1Sign in to save your code
timetopractice
toc
toprac