#18Fruit Basket
A farmer has a long row of fruits represented by a string s. Each character represents a type of fruit. The farmer has a collection requirement represented by another string r. To fill the basket, the farmer needs all the fruits represented in r, including the correct number of each fruit.
Find the smallest continuous section of s that contains every required fruit from r with the required quantity. If multiple sections satisfy the requirement, return the shortest one. If no such section exists, return an empty string.
Examples
Example 1
Input: s = "FFABCFD"
r = "ABC"
Output: "ABC"
Explanation: The farmer needs fruits A, B, and C. The shortest continuous section containing all of them is "ABC".
Example 2
Input: s = "MGKAMNGK"
r = "MAG"
Output: "MGKA"
Explanation: The required fruits are M, A, and G. "GKA" is the shortest section containing all three required fruits.
Example 3
Input: s = "ABACBBAC"
r = "AABC"
Output: "ABAC"
Explanation: The farmer needs two As, one B, and one C. The shortest valid section is "ABAC".
Example 4
Input: s = "XYZAB"
r = "ABZ"
Output: "ZAB"
Explanation: --
Constraints
- 1 ≤ s.length ≤ 10⁵
- 1 ≤ r.length ≤ 10⁵
- s and r consist of uppercase English letters.
- The answer is guaranteed to fit within the length of s.
