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 <= 1001 <= words[i].length <= 101 <= s.length <= 100words[i] and s consist of lowercase English letters.In #2828 Check if a String Is an Acronym of Words, you are given an array of words and a string s. The goal is to verify whether s can be formed by taking the first character of each word in the same order. The main observation is that the length of s must match the number of words; otherwise, it cannot represent their acronym.
A practical approach is to iterate through the list of words and compare each word's first character with the corresponding character in s. If any mismatch occurs, the string cannot be the acronym. This direct comparison avoids unnecessary data structures and keeps the logic simple and efficient.
This approach works in linear time because each word is processed once. Since we only perform character comparisons without storing extra data, the space usage remains constant. The problem mainly tests careful iteration and basic string handling.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Iterate through words and compare first characters with string | O(n) | O(1) |
Prakhar Agrawal
Use these hints if you're stuck. Try solving on your own first.
<div class="_1l1MA">Concatenate the first characters of the strings in <code>words</code>, and compare the resulting concatenation to <code>s</code>.</div>
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
The acronym must contain exactly one character for each word in the array. If the length of the string does not match the number of words, it is impossible for it to represent the acronym.
Problems like this are commonly used as warm-up or screening questions in technical interviews. They test basic string manipulation, iteration, and attention to detail, which are fundamental skills expected in coding interviews.
No complex data structure is required for this problem. A simple array traversal with string indexing is sufficient, making the solution both efficient and easy to implement.
The optimal approach is to iterate through the array of words and compare the first letter of each word with the corresponding character in the string. If all characters match and the lengths are equal, the string forms a valid acronym. This method runs in linear time.