charCounts
of size 26 to store the frequency of each character in chars
.chars
, incrementing the corresponding index in charCounts
.totalLength
to 0, which will store the sum of lengths of all good strings.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
.
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
.totalLength
.