Loading content...
You are given two strings: a pattern string s and a source string t. Your task is to determine whether the pattern can be derived from the source by removing zero or more characters while maintaining the original relative order of the remaining characters.
A derived subsequence is formed by selecting characters from a string in left-to-right order (not necessarily contiguous) without rearranging them. For instance, "ace" can be derived from "abcde" by selecting the characters at positions 0, 2, and 4, but "aec" cannot be derived from "abcde" because it would require rearranging the relative positions of 'e' and 'c'.
Return true if s is a valid derived subsequence of t, otherwise return false.
s = "abc"
t = "ahbgdc"trueThe characters 'a', 'b', and 'c' appear in "ahbgdc" in the same relative order. We can derive "abc" by selecting positions 0, 2, and 5 from the source string.
s = "axc"
t = "ahbgdc"falseWhile 'a' and 'c' exist in "ahbgdc", the character 'x' does not appear in the source string at all. Therefore, "axc" cannot be derived as a subsequence.
s = "ace"
t = "abcde"trueThe pattern "ace" can be derived from "abcde" by selecting the characters at indices 0, 2, and 4. The relative ordering is preserved as 'a' comes before 'c', which comes before 'e'.
Constraints