LeetCode2828. Check if a String Is an Acronym of Words

文章目录

一、题目

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 if s is an acronym of words, and false otherwise.

Example 1:

Input: words = ["alice","bob","charlie"], s = "abc"

Output: true

Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.

Example 2:

Input: words = ["an","apple"], s = "a"

Output: false

Explanation: 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.

Example 3:

Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy"

Output: true

Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy".

Hence, s = "ngguoy" is the acronym.

Constraints:

1 <= words.length <= 100

1 <= words[i].length <= 10

1 <= s.length <= 100

words[i] and s consist of lowercase English letters.

二、题解

cpp 复制代码
class Solution {
public:
    bool isAcronym(vector<string>& words, string s) {
        string s1 = "";
        for(auto word:words){
            s1 += word[0];
        }
        return s1 == s;
    }
};
相关推荐
hope_wisdom1 分钟前
C++网络编程之SSL/TLS加密通信
网络·c++·ssl·tls·加密通信
erxij5 分钟前
【游戏引擎之路】登神长阶(十四)——OpenGL教程:士别三日,当刮目相看
c++·经验分享·游戏·3d·游戏引擎
算法歌者21 分钟前
[算法]入门1.矩阵转置
算法
林开落L36 分钟前
前缀和算法习题篇(上)
c++·算法·leetcode
远望清一色37 分钟前
基于MATLAB边缘检测博文
开发语言·算法·matlab
tyler_download39 分钟前
手撸 chatgpt 大模型:简述 LLM 的架构,算法和训练流程
算法·chatgpt
Prejudices1 小时前
C++如何调用Python脚本
开发语言·c++·python
单音GG1 小时前
推荐一个基于协程的C++(lua)游戏服务器
服务器·c++·游戏·lua
SoraLuna1 小时前
「Mac玩转仓颉内测版7」入门篇7 - Cangjie控制结构(下)
算法·macos·动态规划·cangjie
我狠狠地刷刷刷刷刷1 小时前
中文分词模拟器
开发语言·python·算法