C++ straight forward solution (no need for fancy DP) Runtime: o(N + M) space: o(1)


  • 0
    G
    class Solution {
    public:
        bool isSubsequence(string s, string t) 
        {
            int i = 0;
            int j = 0;
            
            for(;i < s.size() && j < t.size();)
            {
                if(s[i] == t[j])
                {
                    i++;
                    j++;
                }
                else
                {
                    j++;
                }
            }
            
            if(i == s.size())
            {
                return true;
            }
            
            return (false);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.