392. Is Subsequence
Leetcode
題目
Input: s = "abc", t = "ahbgdc"
Output: trueInput: s = "axc", t = "ahbgdc"
Output: false解答
var isSubsequence = function(s, t) {
let indexS = 0;
let indexT = 0;
while(indexS < s.length) {
const char = s[indexS];
const newIndex = t.indexOf(char, indexT);
if(newIndex === -1) return false;
// 從 t 的下個 index 開始找有沒有符合的 char
indexT = newIndex + 1;
indexS++;
}
return true;
};測資
Last updated