#384 Minimum Substring Containing All Characters

Companies:
Yandex
Themes:
AlgorithmsStringsPractice

Write a function that accepts two strings, a text and an alphabet, and returns the shortest substring of the text that contains every character from the alphabet at least once, while possibly containing other characters as well. If several matching substrings have the same length, return the one closest to the beginning of the text.

The second string is guaranteed to be non-empty and to contain only unique characters.

Examples:

searchPangram("abacaba", "bc") == "bac" <- choose the leftmost of the equal-length substrings 'bac' and 'cab'
searchPangram("bacb", "bc") == "cb"
abbc ac => abbc
searchPangram("abacaba", "bcd") == ""

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in