You are given an array of strings words and a string chars.

A string is good if it can be formed by characters from chars (each character can only be used once).

Return the sum of lengths of all good strings in words.

&nbsp;
Example 1:

Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.


Example 2:

Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.


&nbsp;
Constraints:


	1 &lt;= words.length &lt;= 1000
	1 &lt;= words[i].length, chars.length &lt;= 100
	words[i] and chars consist of lowercase English letters.


class Solution(object):
    def countCharacters(self, words, chars):
        """
        :type words: List[str]
        :type chars: str
        :rtype: int
        """
        b = set(chars)
        anwser = 0
        for i in words:
            a = set(i)
            if a.issubset(b):
                test = [o for o in a if chars.count(o) < i.count(o)]
                if len(test) == 0:    
                    anwser += len(i)
        return anwser
class Solution {
    public int countCharacters(String[] words, String chars) {
        int[] freq = new int[26];
        for (int i = 0; i < chars.length(); i++) {
            // char - char is a kind of clever way to get the position of
            // the character in the alphabet. 'a' - 'a' would give you 0.
            // 'b' - 'a' would give you 1. 'c' - 'a' would give you 2, and so on.
            freq[chars.charAt(i) - 'a'] ++;
        }

        int result = 0;
        for (String word : words) {
            int[] copy = Arrays.copyOf(freq, freq.length);
            boolean pass = true;
            for (int j = 0; j < word.length(); j++) {
                // decrement the frequency of this char in array for using
                // if there are less than 1 chance for using this character, invalid,
                // move to next word in words
                if (-- copy[word.charAt(j) - 'a'] < 0) {
                    pass = false;
                    break;
                }
            }
            if (pass) {
                result += word.length();
            }
        }
        return result;
    }
}
class Solution {
public:
    int countCharacters(vector<string>& words, string chars) {
        vector<int> dp(26,0);
        vector<int> dp2(26,0);
        for(int i=0;i<chars.size();i++){
            dp[chars[i]-'a']++;
        }
        dp2 = dp;
        bool flg = false;
        int cnt=0;
        for(int i=0;i<words.size();i++){
            string a = words[i];
            for(int j=0;j<a.size();j++){
                if(dp[a[j]-'a']>0){
                    dp[a[j]-'a']--;
                }
                else{
                    flg = true;
                    dp = dp2;
                    break;
                }
            }
            if(!flg){
               cnt += a.size();
            }
            dp = dp2;
            flg = false;
        }
        return cnt;
    }
};
var countCharacters = function(words, chars) {
    let arr = [];
    loop1: for(word of words){
        let characters = chars;
        loop2: for( char of word ){
            if(characters.indexOf(char) === -1){
                continue loop1;
            }
            characters = characters.replace(char,'');
        }
        arr.push(word);
    }
    return arr.join('').length;
};
Find Words That Can Be Formed by Characters