Easy to understand C++ solution


  • 0
    C
    class Solution {
    public:
        bool isSubsequence(string s, string t) {
            const size_t s_size = s.size();
            const size_t t_size = t.size();
            
            if(s_size > t_size) return false;
            if(s.empty()) return true;
            
            int t_index = 0;
            int s_index = 0;
            while(t_index < t_size && s_index < s_size)
            {
                if(t[t_index] == s[s_index])
                {
                    s_index++;
                }
                t_index++;
            }
            
            return s_index == s_size;
            
            
        }
    };
    

Log in to reply
 

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