Loading problem...
You are given two strings: a source string s and a pattern string t. Your task is to find the shortest contiguous segment within s that contains every character present in t, accounting for their exact frequencies (including duplicate characters).
If no such segment exists in s that satisfies this requirement, return an empty string "".
A contiguous segment (or substring) is defined as a sequence of consecutive characters extracted from the original string, preserving their relative order. For instance, in the string "HELLO", valid substrings include "EL", "LLO", and "HELLO" itself, but "HLO" is not a valid substring because the characters are not consecutive in the original string.
The problem guarantees that if a valid answer exists, it will be unique—there will be exactly one shortest substring covering all characters of t.
Important Clarifications:
t. It may contain additional characters beyond those in t.'A' and 'a' are treated as different characters.t with at least its corresponding frequency. If t = "AAB", any valid covering substring in s must contain at least two 'A's and one 'B'.s = "ADOBECODEBANC"
t = "ABC""BANC"The substring "BANC" is the shortest segment in s that contains all characters from t: 'A', 'B', and 'C'. Other valid covering substrings like "ADOBEC" or "BECODEBA" exist, but "BANC" has the minimum length of 4 characters.
s = "a"
t = "a""a"The entire source string s itself is the smallest (and only) substring that covers all characters in t. Since s contains exactly what t requires, it is returned as the answer.
s = "a"
t = "aa"""The pattern t requires two 'a' characters, but the source string s contains only one 'a'. Since no substring of s can satisfy this requirement, we return an empty string.
Constraints