Given an array of strings words
and a string s
, determine if s
is an acronym of words.
The string s
is considered an acronym of words
if it can be formed by concatenating the first character of each string in words
in order. For example, "ab"
can be formed from ["apple", "banana"]
, but it can't be formed from ["bear", "aardvark"]
.
Return true
if s
is an acronym of words
, and false
otherwise.
Example 1:
Input: words = ["alice","bob","charlie"], s = "abc" Output: true Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.
Example 2:
Input: words = ["an","apple"], s = "a" Output: false Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively. The acronym formed by concatenating these characters is "aa". Hence, s = "a" is not the acronym.
Example 3:
Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy" Output: true Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy". Hence, s = "ngguoy" is the acronym.
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 10
1 <= s.length <= 100
words[i]
and s
consist of lowercase English letters.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 approach to checking if a string is an acronym exhaustively considers every possible way to form the acronym from the words. It's like trying every combination until you find the right one. If no combination produces the target acronym, then it is not possible.
Here's how the algorithm would work step-by-step:
def is_acronym_brute_force(words, acronym):
word_index = 0
acronym_index = 0
while word_index < len(words) and acronym_index < len(acronym):
# Check if the first letter of the current word matches the current letter of the acronym
if words[word_index][0] == acronym[acronym_index]:
acronym_index += 1
word_index += 1
else:
# If it doesn't match, the words cannot form the acronym
return False
#If both word and acronym are exhausted, it is a match
if word_index == len(words) and acronym_index == len(acronym):
return True
else:
#Otherwise, it is not a match.
return False
To check if a string is an acronym of a list of words, we essentially compare the first letter of each word in the list to each letter in the string. We stop if the list of words is exhausted or there's a mismatch between the letters.
Here's how the algorithm would work step-by-step:
def is_acronym(words_list, target_string): word_index = 0
string_index = 0
# Iterate while within bounds of both lists
while word_index < len(words_list) and string_index < len(target_string):
# Compare first char of word to current char in string
if words_list[word_index][0] == target_string[string_index]:
word_index += 1
string_index += 1
else:
# If mismatch, it's not an acronym
return False
# Need to check if we've consumed all words and chars
if word_index == len(words_list) and string_index == len(target_string):
return True
else:
# If either list is not exhausted, not an acronym
return False
Case | How to Handle |
---|---|
words is null or empty | Return true if s is also empty, otherwise return false, as an empty list of words can only produce an empty acronym. |
s is null or empty | Return true if words is also empty, otherwise return false as an empty string cannot be an acronym for a non-empty word list. |
words contains an empty string | If an empty string is in the words array, its first character should be treated as empty, potentially still producing a valid acronym if other words align. |
words contains strings of varying lengths | The solution should correctly extract the first character regardless of the string length. |
s is shorter than the combined length of the first characters of words | The acronym cannot be formed, return false, as s does not contain all the prefixes. |
s is longer than the combined length of the first characters of words | The acronym cannot be formed, return false, as s contains more characters than can be created by prefixes |
words array has very large number of strings, approaching memory limits | The iterative approach avoids deep recursion, thus preventing stack overflow and is memory-efficient. |
words array contains strings with non-alphabetic characters. | The solution should treat each character as is, including special chars/numbers, when comparing with characters in s. |