Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1035565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:33:44+00:00 2026-05-16T14:33:44+00:00

Find minimum window width in string x that contains all characters of another string

  • 0

Find minimum window width in string x that contains all characters of another string y. For example:

String x = "coobdafceeaxab"
String y = "abc"

The answer should be 5, because the shortest substring in x that contains all three letters of y is “bdafc”.

I can think of a naive solution with complexity O(n^2 * log(m)), where n = len(x) and m = len(y). Can anyone suggest a better solution? Thanks.

Update: now think of it, if I change my set to tr1::unordered_map, then I can cut the complexity down to O(n^2), because insertion and deletion should both be O(1).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:33:44+00:00Added an answer on May 16, 2026 at 2:33 pm

    time: O(n) (One pass)
    space: O(k)

    This is how I would do it:
    Create a hash table for all the characters from string Y. (I assume all characters are different in Y).

    First pass:
    Start from first character of string X.
    update hash table, for exa: for key ‘a’ enter location (say 1).
    Keep on doing it until you get all characters from Y (until all key in hash table has value).
    If you get some character again, update its newer value and erase older one.

    Once you have first pass, take smallest value from hash table and biggest value.
    Thats the minimum window observed so far.

    Now, go to next character in string X, update hash table and see if you get smaller window.


    Edit:

    Lets take an example here:
    String x = “coobdafceeaxab”
    String y = “abc”

    First initialize a hash table from characters of Y.
    h[a] = -1
    h[b] = -1
    h[c] = -1

    Now, Start from first character of X.
    First character is c, h[c] = 0
    Second character (o) is not part of hash, skip it.
    ..
    Fourth character (b), h[b] = 3
    ..
    Sixth character(a), enter hash table h[a] = 5.
    Now, all keys from hash table has some value.
    Smallest value is 0 (of c) and highest value is 5 (of a), minimum window so far is 6 (0 to 5).
    First pass is done.

    Take next character. f is not part of hash table, skip it.
    Next character (c), update hash table h[c] = 7.
    Find new window, smallest value is 3 (of b) and highest value is 7 (of c).
    New window is 3 to 7 => 5.

    Keep on doing it till last character of string X.

    I hope its clear now.


    Edit

    There are some concerns about finding max and min value from hash.
    We can maintain sorted Link-list and map it with hash table.
    Whenever any element from Link list changes, it should be re-mapped to hash table.
    Both these operation are O(1)

    Total space would be m+m


    Edit

    Here is small visualisation of above problem:
    For “coobdafceeaxab” and “abc”

    step-0:
    Initial doubly linked-list = NULL
    Initial hash-table = NULL

    step-1:
    Head<->[c,0]<->tail
    h[c] = [0, ‘pointer to c node in LL’]

    step-2:
    Head<->[c,0]<->[b,3]<->tail
    h[c] = [0, ‘pointer to c node in LL’], h[b] = [3, ‘pointer to b node in LL’],

    Step-3:
    Head<->[c,0]<->[b,3]<->[a,5]<->tail
    h[c] = [0, ‘pointer to c node in LL’], h[b] = [3, ‘pointer to b node in LL’], h[a] = [5, ‘pointer to a node in LL’]
    Minimum Window => difference from tail and head => (5-0)+1 => Length: 6

    Step-4:
    Update entry of C to index 7 here. (Remove ‘c’ node from linked-list and append at the tail)
    Head<->[b,3]<->[a,5]<->[c,7]<->tail
    h[c] = [7, ‘new pointer to c node in LL’], h[b] = [3, ‘pointer to b node in LL’], h[a] = [5, ‘pointer to a node in LL’],
    Minimum Window => difference from tail and head => (7-3)+1 => Length: 5

    And so on..

    Note that above Linked-list update and hash table update are both O(1).
    Please correct me if I am wrong..


    Summary:

    TIme complexity: O(n) with one pass
    Space Complexity: O(k) where k is length of string Y

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a string S and a string T, find the minimum window in S
I'm trying to write a program that would find the minimum spanning tree. But
I'm trying to find a 2d array that represents the minimum values of the
I need to find the minimum size that has an aspect ratio of exactly
I have a list of words and i have to find the minimum word
I'm new to the opencv...i want to find the minimum value in the matrix..but
So I need some help coming up with a way to find a Minimum
I need to find out the maximum and minimum value in a line by
Is there any minimum compute capability to use CUDA-OpenGL interoperability? I did not find
Minimum Set Cover is a question where you must find the minimum number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.