061 · Is Subsequence
algorithm
Problem
给定两个字符串 s 和 t。
请判断 s 是否是 t 的子序列。
子序列是指:从原字符串中删除一些字符(也可以一个都不删除),并且不改变剩余字符的相对顺序,得到的新字符串。
例如:
s = "abc"
t = "ahbgdc"
可以从 t 中依次找到:
a, b, c
它们的顺序和 s 相同,所以 s 是 t 的子序列,返回:
true
注意:字符必须保持原来的先后顺序,不能重新排列。
Examples
示例 1
Input: s = "abc", t = "ahbgdc"
Output: true
解释:在 t 中依次保留字符 a、b、c,可以得到 s。
示例 2
Input: s = "axc", t = "ahbgdc"
Output: false
解释:t 中没有字符 x,所以无法按照顺序得到 s。
示例 3
Input: s = "", t = "ahbgdc"
Output: true
解释:空字符串是任意字符串的子序列,因为可以删除原字符串中的所有字符。
Constraints
- \(0 \leq\)
s.length\(\leq 100\) - \(0 \leq\)
t.length\(\leq 10^4\) s和t只由小写英文字母组成
Link
→ Solution