#C2949. Subsequence Checker
Subsequence Checker
Subsequence Checker
You are given two strings, s
and p
. The task is to determine whether p
is a subsequence of s
. A string p
is a subsequence of s
if p
can be derived from s
by deleting zero or more characters without changing the order of the remaining characters.
Example:
- If
s = "abcde"
andp = "ace"
, the answer isYES
. - If
s = "abcde"
andp = "aec"
, the answer isNO
.
The solution should implement an efficient algorithm with a two-pointer technique. Mathematically, the condition for p
to be a subsequence of s
can be stated as:
\[
\exists\, 0 \leq i_1 < i_2 < \cdots < i_k < |s| \quad \text{such that} \quad s[i_1] = p_1, s[i_2] = p_2, \ldots, s[i_k] = p_k
\]
where \( p = p_1p_2\ldots p_k \).
inputFormat
The first line contains an integer T
representing the number of test cases. For each test case, there are two lines:
- The first line contains the string
s
. - The second line contains the string
p
.
outputFormat
For each test case, output a single line containing YES
if p
is a subsequence of s
, or NO
otherwise.
3
abcde
ace
abcde
aec
axc
bx
YES
NO
NO
</p>