Leetcode 2828. Check if a String Is an Acronym of Words
You are given a list of strings words
and a string s
. The task is to determine if s
is an acronym of the strings in words
. A string s
is considered an acronym of words
if it is equal to the concatenation of the first letter of each word in words
in order.
Example:
Output: True
words
and s
always non-empty and contain only alphabetical characters?
words
and s
are non-empty, and each string in words
contains at least one character.words
and s
?
1 <= words.length <= 1000
and the length of each word and s
being <= 1000 characters.words
and concatenate the first letter of each word to the acronym string.s
.n
is the total number of characters in words
. This is because we iterate through each word once and concatenate a single character to form the acronym.#include <vector>
#include <string>
bool isAcronym(std::vector<std::string>& words, std::string s) {
std::string acronym = "";
for (const std::string& word : words) {
acronym += word[0];
}
return acronym == s;
}
This code correctly follows the strategy and checks if s
is an acronym of the words
list.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?