Leetcode – 392. Is Subsequence

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 be…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Leetcode – 392. Is Subsequence." Rakesh Reddy Peddamallu | Sciencx - Thursday September 12, 2024, https://www.scien.cx/2024/09/12/leetcode-392-is-subsequence/
HARVARD
Rakesh Reddy Peddamallu | Sciencx Thursday September 12, 2024 » Leetcode – 392. Is Subsequence., viewed ,<https://www.scien.cx/2024/09/12/leetcode-392-is-subsequence/>
VANCOUVER
Rakesh Reddy Peddamallu | Sciencx - » Leetcode – 392. Is Subsequence. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/12/leetcode-392-is-subsequence/
CHICAGO
" » Leetcode – 392. Is Subsequence." Rakesh Reddy Peddamallu | Sciencx - Accessed . https://www.scien.cx/2024/09/12/leetcode-392-is-subsequence/
IEEE
" » Leetcode – 392. Is Subsequence." Rakesh Reddy Peddamallu | Sciencx [Online]. Available: https://www.scien.cx/2024/09/12/leetcode-392-is-subsequence/. [Accessed: ]
rf:citation
» Leetcode – 392. Is Subsequence | Rakesh Reddy Peddamallu | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.