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;
    }
};
相关推荐
浅念同学1 分钟前
算法.图论-建图/拓扑排序及其拓展
算法·图论
Antonio9154 分钟前
【CMake】使用CMake在Visual Studio内构建多文件夹工程
开发语言·c++·visual studio
是小Y啦18 分钟前
leetcode 106.从中序与后续遍历序列构造二叉树
数据结构·算法·leetcode
LyaJpunov18 分钟前
C++中move和forword的区别
开发语言·c++
程序猿练习生23 分钟前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
liuyang-neu28 分钟前
力扣 42.接雨水
java·算法·leetcode
z千鑫31 分钟前
【人工智能】如何利用AI轻松将java,c++等代码转换为Python语言?程序员必读
java·c++·人工智能·gpt·agent·ai编程·ai工具
一名路过的小码农33 分钟前
C/C++动态库函数导出 windows
c语言·开发语言·c++
y_dd35 分钟前
【machine learning-12-多元线性回归】
算法·机器学习·线性回归
m0_6312704035 分钟前
标准c语言(一)
c语言·开发语言·算法