ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Smallest Substring Containing All Characters of Another String

author
Generated by
Anushka Agrawal

15/11/2024

DSA

Sign in to read full article

Introduction

In string manipulation problems, a frequent challenge is determining the smallest substring that contains all characters from a given string. This is a common problem in coding interviews and can test your understanding of various data structures and algorithms. In this post, we will discuss the problem in detail and provide solutions using a sliding window technique for optimal performance.

Problem Statement

Given two strings, s (the main string) and t (the string containing all characters), the goal is to find the smallest substring in s that contains all characters (including duplicates) from t. If no such substring exists, return an empty string.

Example

  • Input: s = "ADOBECODEBANC", t = "ABC"
  • Output: "BANC"

In the above example, the substring "BANC" contains all characters from "ABC" and is the smallest such substring.

Understanding the Approach

To solve the problem effectively, we will use the sliding window technique. This approach allows us to maintain a window that expands or contracts based on whether the current substring contains all the characters from t.

Steps to Solve the Problem

  1. Character Count:

    • Create a frequency map (or dictionary) for the characters in t.
    • Keep track of how many characters from t are present in the current sliding window.
  2. Sliding Window:

    • Use two pointers (left and right) to create a dynamic window on s.
    • Expand the right pointer to include characters from s.
    • Once all required characters from t are included in the window, attempt to contract the left pointer to find the smallest valid substring.
  3. Condition Check:

    • Check if the current window contains all characters from t by comparing counters or frequency maps.
    • Update the minimum length and starting position whenever you find a smaller valid window.
  4. Result Compilation:

    • After processing the entire string s, extract the smallest valid substring based on the recorded starting position and length.

Implementation

Here is a Python implementation that illustrates this approach:

def min_window(s, t): if not s or not t: return "" # Create a frequency map for characters in t dict_t = {} for char in t: dict_t[char] = dict_t.get(char, 0) + 1 required = len(dict_t) l, r = 0, 0 formed = 0 window_counts = {} min_len = float('inf') min_left = 0 while r < len(s): character = s[r] window_counts[character] = window_counts.get(character, 0) + 1 # Check if the current character fulfills the requirement if character in dict_t and window_counts[character] == dict_t[character]: formed += 1 # Try and contract the window until the point it ceases to be 'desirable' while l <= r and formed == required: character = s[l] # Update minimum length and starting index if r - l + 1 < min_len: min_len = r - l + 1 min_left = l window_counts[character] -= 1 if character in dict_t and window_counts[character] < dict_t[character]: formed -= 1 l += 1 # Keep expanding the right pointer r += 1 return "" if min_len == float('inf') else s[min_left:min_left + min_len] # Test the function s = "ADOBECODEBANC" t = "ABC" print(min_window(s, t)) # Output: "BANC"

Explanation of the Code

  • Character Frequency Count: We establish a frequency dictionary dict_t to keep track of how many times each character in t should appear.
  • Sliding Window Setup: The two pointers l and r represent the start and end of the current window. We also maintain a counter formed that increases when all necessary characters from t are found in the current window.
  • Window Management: When the window contains all required characters, we try to shrink it from the left (by incrementing l) while checking if we can still maintain the conditions needed.
  • Finding the Minimum: Each time we find a valid window, we check if it's the smallest we've seen. If so, we update min_len and min_left to record the position.

This technique provides a highly efficient solution with a time complexity of O(N + M), where N is the length of string s and M is the length of string t.

Using this method, you'll gain not only a strong grasp of the problem but also a toolset to tackle similar string challenges in the future. Keep practicing these techniques, and they will enhance your problem-solving skills significantly!

Popular tags

DSAStringsSliding Window

Share now!

Like & bookmark

Related collections

  • DSA Mastery for Interviews

    23/09/2024 · DSA

  • Trees Interview Questions Using Java

    13/10/2024 · DSA

  • Advanced String-based Interview Techniques

    15/11/2024 · DSA

  • Advanced Graph Interview Questions in Java

    16/11/2024 · DSA

  • Advanced Priority Queue and Heap Interview Questions in Java

    16/11/2024 · DSA

Related articles

  • Sorting Arrays with Custom Comparison Functions in DSA

    06/12/2024 · DSA

  • Word Search in a 2D Grid

    13/10/2024 · DSA

  • Understanding Combination Sum in Advanced Recursion and Backtracking

    13/10/2024 · DSA

  • Working with the AND Operator in Data Structures and Algorithms

    08/12/2024 · DSA

  • Generate All Valid Parentheses

    13/10/2024 · DSA

  • Merge K Sorted Arrays Using Priority Queue

    16/11/2024 · DSA

  • Practical Applications of Bit Manipulation

    08/12/2024 · DSA

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design