Very Simple C++ code for follow up cases

  • 0

    The essential for this problem is that every time we need just find the first available matched character in t.

    bool isSubsequence(string s, string t) {
        vector<vector<int>> char_pos(26);
        for(int i=0; i<t.size(); ++i)
        // remember the previous index of t
        int t_idx = -1;
        for(char c:s){
            int j = c - 'a';
            bool okay = false;
            for(int idx:char_pos[j]){
                if(idx > t_idx){              
                    // found an available character in t
                    okay = true;
                    t_idx = idx;
            if(okay==false) return false;
        return true;

Log in to reply

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