C++ soln with comments


  • 0
    B
    class Solution {
    public:
        bool isSubsequence(string s, string t) {
            if(s.length() == 0) return true;
            if(t.length() == 0) return false;
            int j=0;
            for(int i =0; i< s.length(); ++i){
                // Keep incrementing target string index until a
                // match with input string element is found.
                while(j<t.length() && s[i]!=t[j]) j++;
                // If we reach the end of target string, return false as
                // we couldn't find all elements of input string.
                if(j==t.length()) return false;
                // Go to next index of target array
                j++;            
            }
            // If we reach here, that means we found our sub string
            return true;       
        }
    };
    

Log in to reply
 

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