LeetCode #392 Is Subsequence Solution & Explanation

LeetCode Problem

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., “ace” is a subsequence of “abcde” while “aec” is not).

Follow up: Suppose there are lots of incoming s, say s1, s2, …, sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?


Solution

We can solve this problem with a simple iteration and one parameter.

C# Solution

Solution1

public class Solution {
    public bool IsSubsequence(string s, string t) {
        if(s=="")
            return true;
        char[] arr = s.ToCharArray();
        int idx = 0;

        for(int i = 0; i< t.Length;i++)
        {
            if(t[i]==arr[idx])
            {
                idx+=1;
                if(idx==arr.Length)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

In our solution, the iteration over the characters of string t (in the for loop) allows the code to examine each character in t and compare it with the characters in s.

And the variable idx is used to keep track of the position within string s during this comparison.

Java Solution

Solution1

class Solution {
    public boolean isSubsequence(String s, String t) {
        if(s.equals(""))
            return true;
        char[] arr = s.toCharArray();
        int idx = 0;

        for(int i = 0; i< t.length();i++)
        {
            if(t.charAt(i)==arr[idx])
            {
                idx+=1;
                if(idx==arr.length)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

Python3 Solution

Solution1

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        if s=="":
            return True
        idx = 0

        for i in range(len(t)):
            if(t[i]==s[idx]):
                idx = idx+1
                if(idx==len(s)):
                    return True
        return False    

JavaScript Solution

Solution1

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isSubsequence = function(s, t) {
    if(s=="")
        return true;
    var idx = 0;

    for(let i = 0; i< t.length;i++)
    {
        if(t[i]==s[idx])
        {
            idx+=1;
            if(idx==s.length)
            {
                return true;
            }
        }
    }
    return false;
};

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts or clicking ads, thanks a lot

If you got any problem about the explanation, please feel free to let me know

The problem link : Is Subsequence – LeetCode

Some Random post

Leave a Reply

Your email address will not be published. Required fields are marked *