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;
    }
};
相关推荐
Yupureki1 分钟前
《算法竞赛从入门到国奖》算法基础:动态规划-基础线性dp
c语言·开发语言·算法·动态规划
qq_392807956 分钟前
Qt 注册 C++ 给 QML 调用的几种方式
数据库·c++·qt
Roselind_Yi11 分钟前
从线性回归实战到Python依赖安装踩坑:我的机器学习入门排雷记
笔记·python·算法·机器学习·回归·线性回归·学习方法
Tisfy12 分钟前
LeetCode 3548.等和矩阵分割 II:矩阵旋转 + 哈希表
leetcode·矩阵·散列表·题解·哈希表·矩阵旋转
宵时待雨26 分钟前
C++笔记归纳15:红黑树
开发语言·数据结构·c++·笔记
黑眼圈子27 分钟前
牛客刷题记录5
java·开发语言·学习·算法
具身小佬29 分钟前
两轴机械臂,ros2上位机控制,直接输入坐标或者键盘控制,can通信控制
c++·ubuntu
罗湖老棍子30 分钟前
【例 2】A Simple Problem with Integers(信息学奥赛一本通- P1548)
数据结构·算法·线段树·区间修改 区间查询
abant234 分钟前
leetcode 148 排序链表 归并终极形态
算法·leetcode·链表
cccyi735 分钟前
【C++ 脚手架】Jsoncpp 库的介绍与使用
c++·optional·jsoncpp