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 wordsin order.
For example, "ab" can be formed from ["apple", "banana"], but it can’t be formed from ["bear", "aardvark"].
Return trueifsis an acronym ofwords, andfalseotherwise.
Input: words =["alice","bob","charlie"], s ="abc"Output: trueExplanation: The first character in the words "alice","bob", and "charlie" are 'a','b', and 'c', respectively. Hence, s ="abc"is the acronym.
Input: words =["an","apple"], s ="a"Output: falseExplanation: 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.
Input: words =["never","gonna","give","up","on","you"], s ="ngguoy"Output: trueExplanation: By concatenating the first character of the words in the array, we get the string "ngguoy".Hence, s ="ngguoy"is the acronym.
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.
classSolution {
publicbooleanisAcronym(List<String> words, String s) {
if (words.size() != s.length()) returnfalse;
for (int i = 0; i < words.size(); i++) {
if (words.get(i).charAt(0) != s.charAt(i)) returnfalse;
}
returntrue;
}
}
1
2
3
4
5
6
7
8
classSolution:
defisAcronym(self, words: list[str], s: str) -> bool:
if len(words) != len(s):
returnFalsefor i, w in enumerate(words):
if w[0] != s[i]:
returnFalsereturnTrue