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 for each word in words
).
Return the sum of lengths of all good strings in words.
For example:
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.
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.
Could you please provide an efficient algorithm to solve this problem, considering time and space complexity?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem is all about checking every word individually to see if it can be formed using the provided characters. We examine each word and compare its letters against the available characters to determine if it's a valid word that can be formed.
Here's how the algorithm would work step-by-step:
def find_words_that_can_be_formed_by_characters_brute_force(words, characters):
total_length_of_valid_words = 0
for word in words:
# Check if the current word can be formed
if can_form_word(word, characters):
total_length_of_valid_words += len(word)
return total_length_of_valid_words
def can_form_word(word, characters):
available_characters = list(characters)
for char in word:
# Need to find and remove the character from available chars
if char in available_characters:
available_characters.remove(char)
else:
# If not found, word can't be formed
return False
return True
We want to determine which words can be made using only the characters from a given source string. The clever approach here is to count the characters we have available, and then check if each word can be formed using those counts, avoiding unnecessary string manipulations.
Here's how the algorithm would work step-by-step:
def find_words_that_can_be_formed_by_characters(words, characters):
character_counts = {}
for char in characters:
character_counts[char] = character_counts.get(char, 0) + 1
total_length = 0
for word in words:
word_counts = {}
can_form = True
for char in word:
word_counts[char] = word_counts.get(char, 0) + 1
# Check if the word can be formed
# using the available characters.
for char, count in word_counts.items():
if char not in character_counts or character_counts[char] < count:
can_form = False
break
if can_form:
# Adds the length of the word
# to the total length if it can be formed.
total_length += len(word)
return total_length
Case | How to Handle |
---|---|
words is null or empty | Return 0 as no words can be formed if the input list is empty or null. |
chars is null or empty | Return 0, because no word can be formed if no characters are available. |
words contains empty strings | An empty string has length 0 and should be included in the sum if chars is not empty. |
chars contains repeated characters | The solution should correctly count the occurrences of each character in chars. |
A word in words contains characters not present in chars | The word should not be included in the sum. |
A word contains a character that appears more times than in chars | The word should not be included in the sum. |
words array contains duplicate words | Each word is assessed independently so duplicates do not affect correctness and the lengths are correctly summed without special handling. |
words and chars contain only one type of character (e.g. 'a') | The frequency of 'a' in each word compared to 'a' in chars is verified to ensure correctness. |