Skip to content

Commit

Permalink
Create 1160_Find_Words_That_Can_Be_Formed_by_Characters.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuzaima committed Dec 4, 2023
1 parent d08c300 commit 1ef13ec
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 1160_Find_Words_That_Can_Be_Formed_by_Characters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// id: 1160
// Name: Find Words That Can Be Formed by Characters
// link: https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/
// Difficulty: Easy

class Solution {
int [] map = new int[26];
int [] wordMap;
public int countCharacters(String[] words, String chars) {
for (int i = 0; i < chars.length(); i++) {
map[chars.charAt(i)-'a']++;
}

int result = 0;
for (String word: words) {
if (matches(word)) {
result += word.length();
}
}


return result;
}

private boolean matches (String word) {
wordMap = new int[26];

for (int i = 0; i < word.length(); i++) {
int c = word.charAt(i) - 'a';
wordMap[c]++;
if (wordMap[c]>map[c]) return false;
}

return true;
}
}

0 comments on commit 1ef13ec

Please sign in to comment.