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
    }
}

@@ -12,16 +12,146 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -331,24 +331,405 @@
  0;%0A        
+Map%3CCharacter, List%3CInteger%3E%3E map = new HashMap%3C%3E();%0A        for (int i = 0; i %3C S.length(); i++) %7B%0A            char c = S.charAt(i);%0A            if (!map.containsKey(c)) %7B%0A                List%3CInteger%3E list = new ArrayList%3C%3E();%0A                list.add(i);%0A                map.put(c, list);%0A            %7D else %7B%0A                map.get(c).add(i);%0A            %7D%0A        %7D%0A%0A        
 for (String 
@@ -764,32 +764,17 @@
 int 
-i = 0;%0A            int j
+lastIndex
  = 0
@@ -791,21 +791,28 @@
     
-int count = 0
+boolean found = true
 ;%0A  
@@ -826,16 +826,25 @@
    for (
+int i = 0
 ; i %3C wo
@@ -883,154 +883,280 @@
     
-while (j %3C S.length()) %7B%0A                    if (word.charAt(i) == S.charAt(j++)) %7B%0A                        count++;%0A                        break
+char c = word.charAt(i);%0A                if (!map.containsKey(c)) %7B%0A                    found = false;%0A                    break;%0A                %7D%0A                List%3CInteger%3E indices = map.get(c);%0A                int index = Collections.binarySearch(indices, lastIndex)
 ;%0A  
@@ -1161,37 +1161,48 @@
                 
-    %7D
+if (index %3C 0) %7B
 %0A               
@@ -1194,34 +1194,64 @@
                 
-%7D%0A
+    index = (-(index) - 1);%0A    
                 
@@ -1258,11 +1258,15 @@
 if (
-j =
+index %3E
 = S.
@@ -1269,32 +1269,75 @@
 = S.length()) %7B%0A
+                        found = false;%0A    
                 
@@ -1355,32 +1355,36 @@
                 
+    
 %7D%0A            %7D%0A
@@ -1373,32 +1373,36 @@
   %7D%0A            
+    
 %7D%0A            if
@@ -1403,34 +1403,71 @@
     
-if (count == word.length()
+    lastIndex = index + 1;%0A            %7D%0A%0A            if (found
 ) %7B%0A
@@ -1508,32 +1508,33 @@
     %7D%0A        %7D%0A
+%0A
         return r

Update problem 792