40 lines
958 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* https://leetcode.cn/problems/valid-palindrome/description/?envType=study-plan-v2&envId=top-interview-150
* @param {string} s
* @param {string} t
* @return {boolean}
*/
const isSubsequence = function (s, t) {
};
/*
直接遍历直接遍历t 检查当前s的第一个字符是否在遍历的过程中找到了如果找到了就指向s的第二个字符继续找找到全部找到 返回true
*/
function f1(s, t) {
if (s === t) return true;
let i = 0; // 指向s中要查找的字符
for (const char of t) {
if (char === s[i]) i++;
if (i === s.length) return true;
}
return false;
}
/*
利用传统for提高效率
*/
function f2(s, t) {
if (s === t) return true;
let i = 0; // 指向s中要查找的字符
let j = 0; // 指向t中当前遍历的字符
for(let j = 0;j<t.length,t++) {
}
for (const char of t) {
if (char === s[i]) i++;
if (i === s.length) return true;
}
return false;
}