> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/hashtable/1160.-find-words-that-can-be-formed-by-characters.md).

# 1160. Find Words That Can Be Formed by Characters

![](/files/-MdttmLd0aKKO2C4OF5k)

time: O(n \* k) , n : words array size, k : each word's length

space: O(1)

```java
class Solution {
    public int countCharacters(String[] words, String chars) {
        int map[] = new int[26];
        for (char c : chars.toCharArray()) {
            map[c - 'a']++;
        }
        
        int count = 0;
        for (String word : words) {
            if (isPossible(word, map)) {
                count += word.length();
            }
        }
        return count;
    }
    
    private boolean isPossible(String word, int map[]) {
        int cMap[] = new int[26];
        System.arraycopy(map, 0, cMap, 0, 26); // faster
        // or use
        // int cMap[] = map.clone(); // slower, but easier
        
        for (char c : word.toCharArray()) {
            cMap[c - 'a']--;
            if (cMap[c - 'a'] < 0) return false;
        }
        return true;
    }
}
```
