# 1160. Find Words That Can Be Formed by Characters

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MdqrU0WCKK6ppDxLtty%2F-MdttmLd0aKKO2C4OF5k%2Fimage.png?alt=media\&token=27195629-019d-4951-a540-7ee75420397b)



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;
    }
}
```

