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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s