Tous les défis
Difficile 0 résolus 0 % de réussite

Minimum Window Substring

Find the shortest piece of text containing all required characters.

Problème

Find the shortest contiguous substring of text that contains every character of pattern, including repeats. If several are equally short, return the leftmost.

Implement min_window(text, pattern).

Input

text: The text to search. pattern: The characters that must appear.

Output

Return the shortest window, or an empty string if none exists.

Contraintes

1 ≤ length of text, pattern ≤ 100,000 (ASCII)

Exemples

Exemple 1
Entrée :
text = "ADOBECODEBANC"
pattern = "ABC"

Sortie attendue :
expected = "BANC"
Entrée :
std::string text = "ADOBECODEBANC";
std::string pattern = "ABC";

Sortie attendue :
std::string expected = "BANC";
Entrée :
string text = "ADOBECODEBANC";
string pattern = "ABC";

Sortie attendue :
string expected = "BANC";

Explication

"BANC" contains A, B and C and nothing shorter does.

Exemple 2
Entrée :
text = "a"
pattern = "aa"

Sortie attendue :
expected = ""
Entrée :
std::string text = "a";
std::string pattern = "aa";

Sortie attendue :
std::string expected = "";
Entrée :
string text = "a";
string pattern = "aa";

Sortie attendue :
string expected = "";

Explication

Two a's are needed but the text has only one: "".

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.