LeetCode Problem Workspace
Check if a String Is an Acronym of Words
Check if a string can be formed by concatenating the first letters of each word in a given list.
2
Topics
6
Code langs
3
Related
Practice Focus
Easy · Array plus String
Answer-first summary
Check if a string can be formed by concatenating the first letters of each word in a given list.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array plus String
This problem requires checking if a string is an acronym formed from the first characters of words in an array. By concatenating the first letter of each word and comparing the result to the given string, you can decide if the string matches the acronym pattern. A straightforward approach involves string manipulation and array traversal.
Problem Statement
You are given an array of strings, words, and a string s. Your task is to determine whether s can be formed by concatenating the first character of each word in words in order.
For example, if words = ['alice', 'bob', 'charlie'] and s = 'abc', the first characters of the words are 'a', 'b', and 'c', respectively. Hence, s = 'abc' is a valid acronym. If the first characters do not match the string s, return false.
Examples
Example 1
Input: words = ["alice","bob","charlie"], s = "abc"
Output: true
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
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
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.
Solution Approach
Iterative Approach
Iterate through the words array, extract the first character of each word, and concatenate these characters. Compare the result to s. If they match, return true; otherwise, return false.
String Building
Build the acronym string by joining the first characters of each word in words. Then compare the final string to s to determine if they are identical.
Early Exit Optimization
To optimize, check the lengths of words and s before processing. If their lengths do not match, return false immediately, avoiding unnecessary string building.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The time complexity is O(n), where n is the number of words in the array, since we need to extract the first character from each word. The space complexity is O(1) if we disregard the space used to store the result string.
What Interviewers Usually Probe
- Candidate demonstrates the ability to manipulate arrays and strings efficiently.
- Candidate should be able to recognize the problem's pattern as an acronym check.
- Look for an understanding of early exit conditions to avoid unnecessary work.
Common Pitfalls or Variants
Common pitfalls
- Failing to check the lengths of
sandwordsupfront, leading to unnecessary string building. - Not handling edge cases like when
wordshas only one word or whensis empty. - Not considering the case where the acronym string is longer than
s, causing unnecessary iterations.
Follow-up variants
- Check if a string is an acronym using both uppercase and lowercase letters.
- Implement a version that checks the acronym only for specific words in the list.
- Optimize the solution for handling larger datasets efficiently.
FAQ
What is the solution pattern for 'Check if a String Is an Acronym of Words'?
The problem is an example of using array manipulation and string building, where you concatenate the first letters of the words and compare them to the given string.
How do I optimize this problem for large inputs?
To optimize for large inputs, check the lengths of the words and the acronym string s first. If they don't match, return false immediately without concatenating the characters.
What are the common pitfalls in solving this problem?
A common pitfall is failing to check the lengths of words and s beforehand, leading to unnecessary computations.
What should I do if I encounter an edge case like an empty acronym string?
Handle edge cases by adding checks for empty strings or single-word inputs. Make sure to return false if the lengths do not match.
Can the approach be improved for specific use cases?
Yes, for specific cases such as very large inputs or case-sensitive acronyms, you can optimize the solution by using hash-based or early exit strategies.
Solution
Solution 1: Simulation
We can iterate over each string in the array $words$, concatenate their first letters to form a new string $t$, and then check if $t$ is equal to $s$.
class Solution:
def isAcronym(self, words: List[str], s: str) -> bool:
return "".join(w[0] for w in words) == sSolution 2: Simulation (Space Optimization)
First, we check if the number of strings in $words$ is equal to the length of $s$. If not, $s$ is definitely not an acronym of the first letters of $words$, and we directly return $false$.
class Solution:
def isAcronym(self, words: List[str], s: str) -> bool:
return "".join(w[0] for w in words) == sContinue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array plus String
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward