package leetcode;

/**
 * https://leetcode.com/problems/number-of-matching-subsequences/
 */
public class Problem792 {
    public int numMatchingSubseq(String S, String[] words) {
        int result = 0;
        for (String word : words) {
            int i = 0;
            int j = 0;
            int count = 0;
            for (; i < word.length(); i++) {
                while (j < S.length()) {
                    if (word.charAt(i) == S.charAt(j++)) {
                        count++;
                        break;
                    }
                }
                if (j == S.length()) {
                    break;
                }
            }
            if (count == word.length()) {
                result++;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem792 prob = new Problem792();
        System.out.println(prob.numMatchingSubseq("abcde", new String[]{"a", "bb", "acd", "ace"})); // 3
    }
}

package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/number-of-matching-subsequences/
 */
public class Problem792 {
    public int numMatchingSubseq(String S, String[] words) {
        int result = 0;
        Map<Character, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < S.length(); i++) {
            char c = S.charAt(i);
            if (!map.containsKey(c)) {
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(c, list);
            } else {
                map.get(c).add(i);
            }
        }

        for (String word : words) {
            int lastIndex = 0;
            boolean found = true;
            for (int i = 0; i < word.length(); i++) {
                char c = word.charAt(i);
                if (!map.containsKey(c)) {
                    found = false;
                    break;
                }
                List<Integer> indices = map.get(c);
                int index = Collections.binarySearch(indices, lastIndex);
                if (index < 0) {
                    index = (-(index) - 1);
                    if (index >= S.length()) {
                        found = false;
                        break;
                    }
                }
                lastIndex = index + 1;
            }

            if (found) {
                result++;
            }
        }

        return result;
    }

    public static void main(String[] args) {
        Problem792 prob = new Problem792();
        System.out.println(prob.numMatchingSubseq("abcde", new String[]{"a", "bb", "acd", "ace"})); // 3
    }
}

Update problem 792