Skip to content

2828. Check if a String Is an Acronym of Words 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  bool isAcronym(vector<string>& words, string s) {
    if (words.size() != s.length())
      return false;

    for (int i = 0; i < words.size(); ++i)
      if (words[i][0] != s[i])
        return false;

    return true;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
  public boolean isAcronym(List<String> words, String s) {
    if (words.size() != s.length())
      return false;

    for (int i = 0; i < words.size(); ++i)
      if (words.get(i).charAt(0) != s.charAt(i))
        return false;

    return true;
  }
}
1
2
3
4
class Solution:
  def isAcronym(self, words: list[str], s: str) -> bool:
    return (len(words) == len(s) and
            all(word[0] == c for word, c in zip(words, s)))