LeetCode 2828. 判别首字母缩略词

一、题目

1、题目描述

给你一个字符串数组 words 和一个字符串 s ,请你判断 s 是不是 words 的 首字母缩略词 。

如果可以按顺序串联 words 中每个字符串的第一个字符形成字符串 s ,则认为 s 是 words 的首字母缩略词。例如,"ab" 可以由 ["apple", "banana"] 形成,但是无法从 ["bear", "aardvark"] 形成。

如果 s 是 words 的首字母缩略词,返回 true ;否则,返回 false 。

2、接口描述

class Solution {
public:bool isAcronym(vector<string>& words, string s) {}
};

3、原题链接

2828. 判别首字母缩略词


二、解题报告

1、思路分析

如果单词数目和字符串长度不同直接返回false

否则比对每个单次首字母和相应字符串对应字符,不同则返回false

2、复杂度

时间复杂度: O(n) 空间复杂度:O(1)

3、代码详解

class Solution {
public:bool isAcronym(vector<string>& words, string s) {if(s.size() != words.size()) return false;for(int i = 0 , n = s.size() ; i < n ; i++)if(s[i] != words[i][0]) return false;return true;}
};