> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find Words That Can Be Formed by Characters

> Tested Python solution for LeetCode 1160 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1160, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1160   # by problem number
lcpy gen -s find_words_that_can_be_formed_by_characters   # by problem name
```

## Problem

You are given an array of strings `words` and a string `chars`.

A string is **good** if it can be formed by characters from `chars` (each character can only be used once for **each** word in `words`).

Return *the sum of lengths of all good strings in words*.

### Examples

```
Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
```

```
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
```

### Constraints

* `1 <= words.length <= 1000`
* `1 <= words[i].length, chars.length <= 100`
* `words[i]` and `chars` consist of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_words_that_can_be_formed_by_characters/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_words_that_can_be_formed_by_characters/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(sum(len(w) for w in words) + len(chars))
    # Space: O(1)
    def count_characters(self, words: list[str], chars: str) -> int:
        chars_count = Counter(chars)
        return sum(len(word) for word in words if not (Counter(word) - chars_count))
```

## Complexity

| Time | Space |
| - | - |
| O(sum(len(w) for w in words) + len(chars)) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.