Loading content...
You are given two strings: a source string and a pattern string. Your task is to find the shortest contiguous substring within source such that the pattern is a subsequence of that substring. A subsequence is derived from another string by deleting some or no characters without changing the relative order of the remaining characters.
If there exists no contiguous substring in source that contains pattern as a subsequence, return an empty string "". If multiple substrings of the same minimum length satisfy the condition, return the one that appears earliest (has the smallest starting index) in the source string.
source where pattern exists as a subsequence.For source = "abcdebdde" and pattern = "bde":
"bcde" (indices 1-4) contains "bde" as a subsequence: bcd****e"bdde" (indices 5-8) also contains "bde" as a subsequence: bdd****e"bcde" starts earlier, so we return "bcde"."deb" is NOT valid because the characters must appear in the same order as in pattern.source = "abcdebdde"
pattern = "bde""bcde"The substring "bcde" is the shortest contiguous portion of source that contains "bde" as a subsequence. Although "bdde" also contains "bde" as a subsequence with the same length, "bcde" appears first (starting at index 1 vs index 5), so it is returned. Note that "deb" would not be valid because the characters 'd', 'e', 'b' don't maintain the order 'b', 'd', 'e' from the pattern.
source = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl"
pattern = "u"""The character 'u' does not exist anywhere in the source string, so there is no possible substring that could contain "u" as a subsequence. Therefore, we return an empty string.
source = "abcde"
pattern = "ace""abcde"The pattern "ace" requires characters 'a', 'c', and 'e' to appear in that order. In the source "abcde", these characters are at indices 0, 2, and 4 respectively. The only substring that captures all three in order is the entire string "abcde", which is therefore returned as the shortest covering substring.
Constraints