LeetCode 1160: Find Words That Can Be Formed by Characters Solution

Master LeetCode problem 1160 (Find Words That Can Be Formed by Characters), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1160. Find Words That Can Be Formed by Characters

Problem Explanation

Explanation:

To solve this problem, we can iterate through each word in the words array and check if the characters in that word can be formed using the characters in chars. We can create a frequency map of characters in chars and then for each word, create a frequency map of characters in that word and compare it with the frequency map of chars. If all the characters required for the word are available in chars, we add the length of the word to the result.

  1. Create a frequency map of characters in chars.
  2. For each word in words:
    • Create a frequency map of characters in the word.
    • Check if all characters required for the word are available in the frequency map of chars.
    • If yes, add the length of the word to the result.
  3. Return the final result.

Time Complexity: O(n * m), where n is the number of words and m is the average length of words in the words array.

Space Complexity: O(1) since the frequency maps are constant in size (26 lowercase English letters).

:

Solution Code

class Solution {
    public int countCharacters(String[] words, String chars) {
        int result = 0;
        int[] charFreq = new int[26];
        for (char c : chars.toCharArray()) {
            charFreq[c - 'a']++;
        }
        
        for (String word : words) {
            int[] wordFreq = new int[26];
            boolean validWord = true;
            for (char c : word.toCharArray()) {
                if (wordFreq[c - 'a'] + 1 > charFreq[c - 'a']) {
                    validWord = false;
                    break;
                }
                wordFreq[c - 'a']++;
            }
            if (validWord) {
                result += word.length();
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1160 (Find Words That Can Be Formed by Characters)?

This page provides optimized solutions for LeetCode problem 1160 (Find Words That Can Be Formed by Characters) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1160 (Find Words That Can Be Formed by Characters)?

The time complexity for LeetCode 1160 (Find Words That Can Be Formed by Characters) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1160 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1160 in Java, C++, or Python.

Back to LeetCode Solutions