ยาก 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Minimum Window Substring

Find the shortest piece of text containing all required characters.

โจทย์

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.

เงื่อนไข

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

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
text = "ADOBECODEBANC"
pattern = "ABC"

ผลลัพธ์ที่คาดหวัง:
expected = "BANC"
อินพุต:
std::string text = "ADOBECODEBANC";
std::string pattern = "ABC";

ผลลัพธ์ที่คาดหวัง:
std::string expected = "BANC";
อินพุต:
string text = "ADOBECODEBANC";
string pattern = "ABC";

ผลลัพธ์ที่คาดหวัง:
string expected = "BANC";

คำอธิบาย

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

ตัวอย่างที่ 2
อินพุต:
text = "a"
pattern = "aa"

ผลลัพธ์ที่คาดหวัง:
expected = ""
อินพุต:
std::string text = "a";
std::string pattern = "aa";

ผลลัพธ์ที่คาดหวัง:
std::string expected = "";
อินพุต:
string text = "a";
string pattern = "aa";

ผลลัพธ์ที่คาดหวัง:
string expected = "";

คำอธิบาย

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก