C++ Solution using two pointers


  • 0
    Y

    One pointer for string s, and the other is for string t.

    class Solution {
    public:
        bool isSubsequence(string s, string t) {
            int n = s.size(), m = t.size(), i = 0, j = 0;
            while(i < n && j < m){
                if(s[i] == t[j])
                    ++ i;
                ++ j;
            }
            return i == n;
        }
    };
    

Log in to reply
 

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