Skip to main content
Back to problems
Leetcode
Easy
Arrays
Hash Maps
Strings
Amazon
Find Words That Can Be Formed By Characters

Given a bag of characters, find the total length of all words that can be formed using each character at most as many times as it appears.

Acceptance 0%
Problem Statement

Problem

You are given an array of words and a string of available characters. A word is good if every character in the word can be taken from the available characters without reusing any character more times than it appears in the string.

Return the sum of the lengths of all good words.

A character may be used at most once per occurrence in the given character string. Each word is checked independently against the same character supply.

Goal

Compute the total length of all words that can be formed under these constraints.

Input Format

  • words: an array of lowercase words
  • chars: a lowercase string representing the available characters

Output Format

  • Return a single integer: the sum of lengths of all words that can be formed from chars.

Constraints

  • 1 <= words.length is typically up to the low thousands
  • Words and chars contain only lowercase English letters
  • Exact official constraints may vary by platform; use frequency counting to solve efficiently
Examples
Sample cases returned by the problem API.

Example 1

Input

words = ["cat","bt","hat","tree"], chars = "atach"

Output

6

Explanation

"cat" and "hat" can be formed, so the total is 3 + 3 = 6.

Example 2

Input

words = ["hello","world","leetcode"], chars = "welldonehoneyr"

Output

10

Explanation

"hello" and "world" can be formed. Their lengths are 5 and 5, so the sum is 10.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.