VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-smallest-window-in-a-string-containing-all-characters-of-another-string/

⇱ Smallest window in a String containing all characters of other String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Smallest window in a String containing all characters of other String

Last Updated : 27 Feb, 2026

Given two strings s and p, the task is to find the smallest substring in s that contains all characters of p, including duplicates. If no such substring exists, return "". If multiple substrings of the same length are found, return the one with the smallest starting index.

Examples:

Input: s = "timetopractice", p = "toc"
Output: toprac
Explanation: "toprac" is the smallest substring in which "toc" can be found.

Input: s = "zoomlazapzo", p = "oza"
Output: apzo
Explanation: "apzo" is the smallest substring in which "oza" can be found.

[Naive Approach] By Generating all the Substrings - O(n^3) time and O(n) space:

The very basic idea to solve this problem is that we can generate all possible substrings of the given string s and checking each substring to see if it contains all characters of string p. This checking can be done by a helper function that counts the frequency of each character in p equals with frequency of the chosen substring. If a substring contains all characters of the p, then its length is compared to the current minimum length and the smallest substring is updated accordingly. The process continues until all substrings have been checked.


Output
toprac

[Better Approach] By using Binary Search on Answer - O(n*log(n)) Time and O(1) Space:

The idea is to check if a window of a certain size "mid" is valid (contains all characters of the p string), then all windows of size greater than "mid" will also be valid. Similarly, if a window of size "mid" is not valid, then all windows of size smaller than "mid" will also not be valid. This property allows us to apply binary search effectively.

Follow the steps below to solve the problem:

  • Initialize low = 1 and high = string length. Denoting the minimum and maximum possible answer.
  • For any value mid check if there is any substring of length mid in the string that contains all the characters of the P.
    • If any such substring of length exists then store the starting index of that substring and update high to mid-1 and, check for substrings having lengths smaller than mid.
    • Otherwise, if any such substring does not exist then update low to mid+1 and, check for substrings having lengths larger than mid.

Output
toprac

[Expected Approach] Using Window Sliding - O(n) Time and O(1) Space:

The idea is to use Window Sliding (start and j) to maintain a sliding window over string S, while tracking character frequencies with two count arrays.

Steps:

  1. Initialize:
    • A count array to store the frequency of characters in P.
    • Another count array to track the characters in the current window of S.
    • Variables to track the minimum window length and its start index.
  2. Expand the Window:
    • Move the j pointer through S, updating the window's character counts.
    • When all characters of P are present in the window, a valid window is found.
  3. Shrink the Window before updating result
    • Move the start pointer right to minimize the window while ensuring all characters from P remain in the window.
    • Track the smallest window during this process.
  4. Return Result:
    • If a valid window is found, return the smallest substring. If no valid window exists, return "-1".

Illustration:


Output
toprac
Comment