bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1160. Find Words That Can Be Formed by Characters

1160. Find Words That Can Be Formed by Characters

Leetcode Solutions

Count With Array

  1. Initialize an array charCounts of size 26 to store the frequency of each character in chars.
  2. Iterate over each character in chars, incrementing the corresponding index in charCounts.
  3. Initialize totalLength to 0, which will store the sum of lengths of all good strings.
  4. For each word in words, do the following: a. Initialize an array wordCounts of size 26 to store the frequency of each character in the current word. b. Iterate over each character in the word, incrementing the corresponding index in wordCounts. c. Initialize a boolean canForm to true. d. Iterate over each index in wordCounts.
    • If wordCounts[i] is greater than charCounts[i], set canForm to false and break the loop. e. If canForm is true, add the length of the word to totalLength.
  5. Return totalLength.
UML Thumbnail

Count With Hash Map

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...