注:"ace"是"abcde"的一个子序列,而"aec"不是
示例 1:
输入:s = "abc", t = "ahbgdc"
输出:true
示例 2:
输入:s = "axc", t = "ahbgdc"
输出:false
func isSubsequence(s string, t string) bool {
//记录字符串长度
lenth := len(s)
//判断字符长度<1的情况
if lenth <= 1 && strings.Contains(t, s) {
return true
}
//遍历字符串
for i, _ := range t {
//如果s为0说明匹配完了
if lenth == 0 {
break
}
//如果字符相等,则s删除匹配过的字符,长度-1
if lenth != 0 && t[i] == s[0] {
s = s[1:]
lenth--
}
}
//返回判断
return lenth == 0
}