Given two strings a and b, determine whether a can be converted into b by removing zero or more characters from a without rearranging the remaining characters.
Return True if b appears as a subsequence of a, and False otherwise.
Function Signature
def can_convert(a: str, b: str) -> bool:
Rules
-
Characters are compared exactly, so
"A"
and
"a"
are different characters.
-
The empty string is a subsequence of every string, including the empty string, so an empty
b
always gives
True
.
-
A non-empty
b
can never be obtained from an empty
a
.
Constraints
-
0 <= len(a), len(b) <= 10^5
-
a
and
b
consist of printable ASCII characters.
Examples
Input: a = "abcde", b = "ace"
Output: True
Removing b and d from "abcde" gives "ace".
Input: a = "abc", b = "acb"
Output: False
Characters cannot be reordered. After matching a and then c (the last character of "abc"), nothing is left to match the final b.
Input: a = "xyz", b = ""
Output: True
Removing all three characters of "xyz" leaves the empty string.