Alle Herausforderungen
Schwierig 0 gelöst 0% Erfolgsquote

Minimum Window Substring

Find the shortest piece of text containing all required characters.

Problem

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.

Einschränkungen

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

Beispiele

Beispiel 1
Eingabe:
text = "ADOBECODEBANC"
pattern = "ABC"

Erwartete Ausgabe:
expected = "BANC"
Eingabe:
std::string text = "ADOBECODEBANC";
std::string pattern = "ABC";

Erwartete Ausgabe:
std::string expected = "BANC";
Eingabe:
string text = "ADOBECODEBANC";
string pattern = "ABC";

Erwartete Ausgabe:
string expected = "BANC";

Erläuterung

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

Beispiel 2
Eingabe:
text = "a"
pattern = "aa"

Erwartete Ausgabe:
expected = ""
Eingabe:
std::string text = "a";
std::string pattern = "aa";

Erwartete Ausgabe:
std::string expected = "";
Eingabe:
string text = "a";
string pattern = "aa";

Erwartete Ausgabe:
string expected = "";

Erläuterung

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.