This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu
Since we need to check every character of s to be sequentially present in t , if we find the character of s in t , then we can find the next char of s in the subsequent part of t ie we now don't need to go through every char of t . this is the idea behind the given code
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isSubsequence = function(s, t) {
for(c of s){
let index = t.indexOf(c);
if(index===-1){
return false;
}
t = t.substring(index+1);
}
return true;
};
This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu | Sciencx (2024-09-12T12:27:24+00:00) Leetcode – 392. Is Subsequence. Retrieved from https://www.scien.cx/2024/09/12/leetcode-392-is-subsequence/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.