題目描述:
Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
給兩個(gè)字符串s和t抢呆,查看s是否是t的不連續(xù)子序列浑塞。
題目分析:
該題可以用On的時(shí)間復(fù)雜度來(lái)算木羹,遍歷t字符串播揪,每次匹配到s的字符時(shí)將s里的字符去掉。遍歷完成后荚醒,檢查s字符串是否有剩余滓技,如果沒(méi)有剩余,則說(shuō)明s在t中存在不連續(xù)子序列冯吓。
代碼:
var isSubsequence = function(s, t) {
if(s === t) {
return true
}
var strS = s
for(let i = 0; i < t.length; i++) {
if(strS.length > 0 && strS[0] === t[i]){
strS = strS.slice(1)
}
}
return strS.length === 0
};