Unknown's avatar

1160. Find Words That Can Be Formed by Characters

1160. Find Words That Can Be Formed by Characters

class Solution {
		public int countCharacters(String[] words, String chars) {
		HashMap freq = new HashMap();
		for (int i = 0; i < chars.length(); i++) {
			freq.put(chars.charAt(i), freq.getOrDefault(chars.charAt(i), 0) + 1);
		}
		int count = 0;
		for (int i = 0; i < words.length; i++) {
			HashMap freq1 = new HashMap(freq);
			boolean charsNotFound = false;
			for (int j = 0; j  0)
					freq1.put(words[i].charAt(j), freq1.get(words[i].charAt(j)) - 1);
				else {
					charsNotFound = true;
					break;
				}
			}
			if (!charsNotFound)
				count += words[i].length();
		}
		return count;
	}
}

Leave a comment