Smallest Window Problem Statement

Given two strings, S and X, your task is to find the smallest substring in S that contains all the characters present in X.

Example:

Input:
S = "abdd", X = "bd"
Output:
"bd"
Explanation:

The substrings in S that contain all the characters in X are: "abdd", "abd", "bdd", "bd". Among these, "bd" is the smallest substring.

Constraints:

  • 1 <= T <= 10
  • 1 <= |S|, |X| <= 10^5
  • Time Limit: 1 sec

Input:

The first line contains an integer T, the number of test cases.
Each test case consists of two lines:
- First line contains the string S.
- Second line contains the string X.

Output:

For each test case, print the smallest window in S containing all characters of X, on a separate line.

Note:

  • There is always a valid window in S that contains all characters of X.
  • If there are multiple smallest windows, return the one that appears first.
Be the first one to answer
Add answer anonymously...
MakeMyTrip Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter