392. Is Subsequence - Easy

 Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that is formed from the original characters without disturbing the relative positions of the remaining characters e.g "ace" is a subsequence of "abcde" while "aec" is not.

Example:
Input: s = "abc", t = "ahbgdc"

Output: true

Constraints:
0 <= s.length <= 100

0 <= t.length <= 10**4

s and t consist only of lowercase English letters

Solution:
var isSubsequence = function(s, t) {

    let index = 0;

    let string = '';

    for (let i = 0; i < t.length; i++) {
        if (s[index] === t[i]) {

            string += t[i];

            index++;

        }

    }

     return string === s;

}

Thoughts:

Figured out the solution on my own, which is great. It's not the most efficient solution but I'm happy with it and would give myself a 8/10 for confidence. I just need to build my confidence up and do more problems to be comfortable doing these under time pressure and sharing my screen.

Comments

Popular posts from this blog

28. Find the Index of the First Occurence in a String - Easy

121. Best Time to Buy and Sell Stock - Easy

58. Length of Last Word - Easy