Problem
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
ifs
is an acronym ofwords
, andfalse
otherwise.
Examples
Example 1
|
|
Example 2
|
|
Example 3
|
|
Constraints
1 <= words.length <= 100
1 <= words[i].length <= 10
1 <= s.length <= 100
words[i]
ands
consist of lowercase English letters.
Solution
Method 1 – Direct Character Comparison
Intuition: To check if a string is an acronym of a list of words, compare the string to the concatenation of the first character of each word in order. If they match, return true; otherwise, return false.
Approach:
- If the length of
s
is not equal to the number of words, return false. - For each word, compare its first character to the corresponding character in
s
. - If all characters match, return true; otherwise, return false.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the number of words. - 🧺 Space complexity:
O(1)