Tutte le sfide
Difficile 0 risolte 0% di accettazione

Minimum Window Substring

Find the shortest piece of text containing all required characters.

Problema

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.

Vincoli

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

Esempi

Esempio 1
Input:
text = "ADOBECODEBANC"
pattern = "ABC"

Output atteso:
expected = "BANC"
Input:
std::string text = "ADOBECODEBANC";
std::string pattern = "ABC";

Output atteso:
std::string expected = "BANC";
Input:
string text = "ADOBECODEBANC";
string pattern = "ABC";

Output atteso:
string expected = "BANC";

Spiegazione

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

Esempio 2
Input:
text = "a"
pattern = "aa"

Output atteso:
expected = ""
Input:
std::string text = "a";
std::string pattern = "aa";

Output atteso:
std::string expected = "";
Input:
string text = "a";
string pattern = "aa";

Output atteso:
string expected = "";

Spiegazione

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.