package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/word-subsets/
 */
public class Problem916 {
    public List<String> wordSubsets(String[] A, String[] B) {
        List<String> result = new ArrayList<>();
        outer:
        for (String a : A) {
            for (String b : B) {
                int[] counts = new int[26];
                for (char c : a.toCharArray()) {
                    counts[c - 'a']++;
                }
                for (char c : b.toCharArray()) {
                    if (counts[c - 'a'] == 0) {
                        continue outer;
                    }
                    counts[c - 'a']--;
                }
            }
            result.add(a);
        }
        return result;
    }

    public static void main(String[] args) {
        Problem916 prob = new Problem916();
        System.out.println(prob.wordSubsets(
            new String[]{"amazon", "apple", "facebook", "google", "leetcode"},
            new String[]{"e", "o"})); // ["facebook","google","leetcode"]
        System.out.println(prob.wordSubsets(
            new String[]{"amazon", "apple", "facebook", "google", "leetcode"},
            new String[]{"l", "e"})); // ["apple","google","leetcode"]
        System.out.println(prob.wordSubsets(
            new String[]{"amazon", "apple", "facebook", "google", "leetcode"},
            new String[]{"e", "oo"})); // ["facebook","google"]
        System.out.println(prob.wordSubsets(
            new String[]{"amazon", "apple", "facebook", "google", "leetcode"},
            new String[]{"lo", "eo"})); // ["google","leetcode"]
        System.out.println(prob.wordSubsets(
            new String[]{"amazon", "apple", "facebook", "google", "leetcode"},
            new String[]{"ec", "oc", "ceo"})); // ["facebook","leetcode"]
    }
}

@@ -268,48 +268,36 @@
     
-outer:%0A        for (String a : A) %7B%0A    
+int%5B%5D countB = new int%5B26%5D;%0A
     
@@ -337,24 +337,17 @@
     
-    
 int%5B%5D 
-counts
+tmp
  = n
@@ -354,28 +354,24 @@
 ew int%5B26%5D;%0A
-    
             
@@ -384,17 +384,17 @@
 har c : 
-a
+b
 .toCharA
@@ -415,34 +415,27 @@
             
-   
- counts
+tmp
 %5Bc - 'a'%5D++;
@@ -455,89 +455,41 @@
     
-%7D%0A                for (char c : b.toCharArray()) %7B%0A                    if 
+countB%5Bc - 'a'%5D = Math.max
 (count
-s
+B
 %5Bc -
@@ -497,105 +497,13 @@
 'a'%5D
- == 0) %7B%0A                        continue outer;%0A                    %7D%0A                    counts
+, tmp
 %5Bc -
@@ -511,14 +511,11 @@
 'a'%5D
---;%0A  
+);%0A
     
@@ -522,31 +522,26 @@
         
-  %7D%0A   
+%7D%0A
         
  %7D%0A     
@@ -524,33 +524,32 @@
       %7D%0A        
- 
 %7D%0A            re
@@ -546,1123 +546,436 @@
     
-   
- result.add(a);%0A        %7D%0A        return result;
+outer:
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem916 prob = new Problem916();%0A        System.out.println(prob.wordSubsets(%0A            new String%5B%5D%7B%22amazon%22, %22apple%22, %22facebook%22, %22google%22, %22leetcode%22%7D,%0A            new String%5B%5D%7B%22e%22, %22o%22%7D)); // %5B%22facebook%22,%22google%22,%22leetcode%22%5D%0A        System.out.println(prob.wordSubsets(%0A            new String%5B%5D%7B%22amazon%22, %22apple%22, %22facebook%22, %22google%22, %22leetcode%22%7D,%0A            new String%5B%5D%7B%22l%22, %22e%22%7D)); // %5B%22apple%22,%22google%22,%22leetcode%22%5D%0A        System.out.println(prob.wordSubsets(%0A            new String%5B%5D%7B%22amazon%22, %22apple%22, %22facebook%22, %22google%22, %22leetcode%22%7D,%0A            new String%5B%5D%7B%22e%22, %22oo%22%7D)); // %5B%22facebook%22,%22google%22%5D%0A        System.out.println(prob.wordSubsets(%0A            new String%5B%5D%7B%22amazon%22, %22apple%22, %22facebook%22, %22google%22, %22leetcode%22%7D,%0A            new String%5B%5D%7B%22lo%22, %22eo%22%7D)); // %5B%22google%22,%22leetcode%22%5D%0A        System.out.println(prob.wordSubsets(%0A            new String%5B%5D%7B%22amazon%22, %22apple%22, %22facebook%22, %22google%22, %22leetcode%22%7D,%0A            new String%5B%5D%7B%22ec%22, %22oc%22, %22ceo%22%7D)); // %5B%22facebook%22,%22leetcode%22%5D
+for (String a : A) %7B%0A            int%5B%5D countA = new int%5B26%5D;%0A            for (char c : a.toCharArray()) %7B%0A                countA%5Bc - 'a'%5D++;%0A            %7D%0A            for (int i = 0; i %3C countB.length; i++) %7B%0A                countA%5Bi%5D -= countB%5Bi%5D;%0A                if (countA%5Bi%5D %3C 0) %7B%0A                    continue outer;%0A                %7D%0A            %7D%0A            result.add(a);%0A        %7D%0A        return result;
 %0A   

Solve problem 916