Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1160. Find Words That Can Be Formed by Characters

Easy70.8% Acceptance
ArrayHash TableString
Asked by:
K
Karat
ProblemHints (4)Solutions (8)VideosCompanies (4)Notes

Problem Statement

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).

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

Example 1:

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.

Example 2:

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.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
I
Indeed
A
Amazon
G
Google

Approach

In #1160 Find Words That Can Be Formed by Characters, the goal is to determine which words in a list can be constructed using the characters from a given string. Each character in the source string can only be used as many times as it appears. The key idea is to track character frequencies.

A common and efficient approach is to use a frequency counter (such as an array of size 26 or a hash table) to store the count of each character in chars. For every word in the list, create a temporary copy of this frequency structure and decrement counts as characters are used. If any character’s count becomes negative, that word cannot be formed.

If the word is valid, add its length to the total result. This counting technique avoids expensive repeated searches and keeps the solution efficient. The overall approach mainly involves iterating through the words and performing character counting checks, resulting in linear behavior relative to the total input size.

Complexity

ApproachTime ComplexitySpace Complexity
Character Frequency Counting (Array / Hash Map)O(W * K + C)O(1)

Video Solution Available

Nick White

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Solve the problem for each string in <code>words</code> independently.

2
Hint 2

Now try to think in frequency of letters.

3
Hint 3

Count how many times each character occurs in string <code>chars</code>.

4
Hint 4

To form a string using characters from <code>chars</code>, the frequency of each character in <code>chars</code> must be greater than or equal the frequency of that character in the string to be formed.

Ready to see the solutions?View Solutions

Solutions (8)

Character Frequency Counting

This approach involves counting the frequency of each character in the 'chars' string. For each word, check if the word can be constructed using these characters, respecting their frequencies.

Time Complexity: O(N*K + C), where N = number of words, K = average length of the word, C = length of 'chars'.
Space Complexity: O(1), as the auxiliary space used is constant, i.e., the two arrays of size 26.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4int countCharacters(char **words, int wordsSize, char *chars) {

Explanation

This solution counts the frequency of each character in the 'chars' array using an integer array of size 26. For each word, it creates a frequency array to check if the word can be formed without exceeding the available characters and their counts.

Map-based Character Counting

This approach utilizes hash maps to count the frequency of characters in both the 'chars' string and each word. The map allows for dynamic sizing and flexibility with character counts.

Time Complexity: O(N*K + C), where N = number of words, K = average length of the word, C = length of 'chars'.
Space Complexity: O(1), additional space overhead is minimal with the unordered maps.

C++Python
1#include <iostream>
2#include <vector>
3#include <string>
4#include <unordered_map>
5using namespace std;

int countCharacters(vector<string>& words, string chars) {
    unordered_map<char, int> charMap;
    for (char c : chars) {
        charMap[c]++;
    }
    int result = 0;
    for (string word : words) {
        unordered_map<char, int> wordMap;
        for (char c : word) {
            wordMap[c]++;
        }
        bool canForm = true;
        for (auto& entry : wordMap) {
            if (entry.second > charMap[entry.first]) {
                canForm = false;
                break;
            }
        }
        if (canForm) {
            result += word.length();
        }
    }
    return result;
}

int main() {
    vector<string> words = {"cat", "bt", "hat", "tree"};
    string chars = "atach";
    cout << countCharacters(words, chars) << endl;  // Output: 6
    return 0;
}

Video Solutions

Watch expert explanations and walkthroughs

LeetCode Find Words That Can Be Formed by Characters Solution Explained - Java

Nick White
8:2011,826 views

Asked By Companies

4 companies
K
Karat
I
Indeed
A
Amazon
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Two SumEasy
Valid SudokuMedium
Sudoku SolverHard
First Missing PositiveHard
More similar problems

Related Topics

ArrayHash TableStringCounting

Problem Stats

Acceptance Rate70.8%
DifficultyEasy
Companies4

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why is counting used in Find Words That Can Be Formed by Characters?

Counting helps efficiently track how many times each character can be used. By decrementing counts while checking a word, we can quickly detect if the word requires more characters than available.

Is Find Words That Can Be Formed by Characters asked in FAANG interviews?

While the exact problem may not always appear, similar frequency counting and string validation problems are common in FAANG-style interviews. Practicing this problem helps build a strong foundation in hash tables and counting techniques.

What data structure is best for Find Words That Can Be Formed by Characters?

A fixed-size frequency array of length 26 is usually the best choice because the input consists of lowercase English letters. A hash map can also work, but the array is faster and uses constant space.

What is the optimal approach for Find Words That Can Be Formed by Characters?

The optimal approach uses a character frequency counter for the given chars string. For each word, compare its character usage with the available counts and ensure no character is used more times than allowed.

5
int
charCount
[
26
]
=
{
0
}
;
6
for
(
int
i
=
0
;
chars
[
i
]
!=
'\0'
;
i
++
)
{
7
charCount
[
chars
[
i
]
-
'a'
]
++
;
8
}
9
int
result
=
0
;
10
for
(
int
i
=
0
;
i
<
wordsSize
;
i
++
)
{
11
int
wordCount
[
26
]
=
{
0
}
;
12
for
(
int
j
=
0
;
words
[
i
]
[
j
]
!=
'\0'
;
j
++
)
{
13
wordCount
[
words
[
i
]
[
j
]
-
'a'
]
++
;
14
}
15
int
canForm
=
1
;
16
for
(
int
k
=
0
;
k
<
26
;
k
++
)
{
17
if
(
wordCount
[
k
]
>
charCount
[
k
]
)
{
18
canForm
=
0
;
19
break
;
20
}
21
}
22
if
(
canForm
)
{
23
result
+=
strlen
(
words
[
i
]
)
;
24
}
25
}
26
return
result
;
27
}
28
29
int
main
(
)
{
30
char
*
words
[
]
=
{
"cat"
,
"bt"
,
"hat"
,
"tree"
}
;
31
char
chars
[
]
=
"atach"
;
32
int
wordsSize
=
4
;
33
printf
(
"%d\n"
,
countCharacters
(
words
,
wordsSize
,
chars
)
)
;
// Output: 6
34
return
0
;
35
}
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

Explanation

This solution counts the frequencies of characters using hash maps for both 'chars' and each word. It checks if the words can be formed by comparing counts in these maps and adds the lengths of words that can be formed.