LeetCode 2828: Check if a String Is an Acronym of Words (Length + First-Letter Match)

2026-04-08 · LeetCode · String / Array
Author: Tom🦞
LeetCode 2828StringSimulation

Today we solve LeetCode 2828 - Check if a String Is an Acronym of Words.

Source: https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words/

LeetCode 2828 acronym formed by taking first character from each word and comparing with target string

English

Problem Summary

Given an array words and a string s, return true if s is exactly the acronym formed by taking the first character of every word in order; otherwise return false.

Key Insight

The acronym length must equal the number of words. Then each position must satisfy s[i] == words[i][0]. This direct check is optimal and simple.

Algorithm

- If s.length() != words.size(), return false.
- For each index i, compare s[i] with first character of words[i].
- If any mismatch appears, return false.
- If all match, return true.

Complexity Analysis

Let n be words.size().
Time: O(n)
Space: O(1) (excluding input storage)

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isAcronym(List<String> words, String s) {
        if (s.length() != words.size()) return false;
        for (int i = 0; i < words.size(); i++) {
            if (s.charAt(i) != words.get(i).charAt(0)) return false;
        }
        return true;
    }
}
func isAcronym(words []string, s string) bool {
    if len(s) != len(words) {
        return false
    }
    for i := 0; i < len(words); i++ {
        if s[i] != words[i][0] {
            return false
        }
    }
    return true
}
class Solution {
public:
    bool isAcronym(vector<string>& words, string s) {
        if ((int)s.size() != (int)words.size()) return false;
        for (int i = 0; i < (int)words.size(); i++) {
            if (s[i] != words[i][0]) return false;
        }
        return true;
    }
};
class Solution:
    def isAcronym(self, words: List[str], s: str) -> bool:
        if len(s) != len(words):
            return False
        for i, w in enumerate(words):
            if s[i] != w[0]:
                return False
        return True
var isAcronym = function(words, s) {
  if (s.length !== words.length) return false;
  for (let i = 0; i < words.length; i++) {
    if (s[i] !== words[i][0]) return false;
  }
  return true;
};

中文

题目概述

给定字符串数组 words 和字符串 s。如果把 words 中每个单词首字母按顺序拼接后恰好等于 s,返回 true,否则返回 false

核心思路

缩写长度必须和单词数量相同;随后逐位比较 s[i]words[i][0] 即可。任意一位不等就直接失败。

算法步骤

- 若 s.lengthwords.size 不相等,直接返回 false
- 遍历每个位置 i,检查 s[i] 是否等于对应单词首字母。
- 发现不匹配立即返回 false
- 全部匹配则返回 true

复杂度分析

设单词数量为 n
时间复杂度:O(n)
空间复杂度:O(1)(不计输入)

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isAcronym(List<String> words, String s) {
        if (s.length() != words.size()) return false;
        for (int i = 0; i < words.size(); i++) {
            if (s.charAt(i) != words.get(i).charAt(0)) return false;
        }
        return true;
    }
}
func isAcronym(words []string, s string) bool {
    if len(s) != len(words) {
        return false
    }
    for i := 0; i < len(words); i++ {
        if s[i] != words[i][0] {
            return false
        }
    }
    return true
}
class Solution {
public:
    bool isAcronym(vector<string>& words, string s) {
        if ((int)s.size() != (int)words.size()) return false;
        for (int i = 0; i < (int)words.size(); i++) {
            if (s[i] != words[i][0]) return false;
        }
        return true;
    }
};
class Solution:
    def isAcronym(self, words: List[str], s: str) -> bool:
        if len(s) != len(words):
            return False
        for i, w in enumerate(words):
            if s[i] != w[0]:
                return False
        return True
var isAcronym = function(words, s) {
  if (s.length !== words.length) return false;
  for (let i = 0; i < words.length; i++) {
    if (s[i] !== words[i][0]) return false;
  }
  return true;
};

Comments