{"id":10082,"date":"2023-08-26T09:29:43","date_gmt":"2023-08-26T16:29:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10082"},"modified":"2023-08-26T09:30:20","modified_gmt":"2023-08-26T16:30:20","slug":"leetcode-2828-check-if-a-string-is-an-acronym-of-words","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2828-check-if-a-string-is-an-acronym-of-words\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2828. Check if a String Is an Acronym of Words"},"content":{"rendered":"\n<p>Given an array of strings&nbsp;<code>words<\/code>&nbsp;and a string&nbsp;<code>s<\/code>, determine if&nbsp;<code>s<\/code>&nbsp;is an&nbsp;<strong>acronym<\/strong>&nbsp;of words.<\/p>\n\n\n\n<p>The string&nbsp;<code>s<\/code>&nbsp;is considered an acronym of&nbsp;<code>words<\/code>&nbsp;if it can be formed by concatenating the&nbsp;<strong>first<\/strong>&nbsp;character of each string in&nbsp;<code>words<\/code>&nbsp;<strong>in order<\/strong>. For example,&nbsp;<code>\"ab\"<\/code>&nbsp;can be formed from&nbsp;<code>[\"apple\", \"banana\"]<\/code>, but it can&#8217;t be formed from&nbsp;<code>[\"bear\", \"aardvark\"]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>s<\/code><em>&nbsp;is an acronym of&nbsp;<\/em><code>words<\/code><em>, and&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"alice\",\"bob\",\"charlie\"], s = \"abc\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The first character in the words \"alice\", \"bob\", and \"charlie\" are 'a', 'b', and 'c', respectively. Hence, s = \"abc\" is the acronym. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"an\",\"apple\"], s = \"a\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> The first character in the words \"an\" and \"apple\" are 'a' and 'a', respectively. \nThe acronym formed by concatenating these characters is \"aa\". \nHence, s = \"a\" is not the acronym.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"never\",\"gonna\",\"give\",\"up\",\"on\",\"you\"], s = \"ngguoy\"\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>By concatenating the first character of the words in the array, we get the string \"ngguoy\". \nHence, s = \"ngguoy\" is the acronym.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= words.length &lt;= 100<\/code><\/li><li><code>1 &lt;= words[i].length &lt;= 10<\/code><\/li><li><code>1 &lt;= s.length &lt;= 100<\/code><\/li><li><code>words[i]<\/code>&nbsp;and&nbsp;<code>s<\/code>&nbsp;consist of lowercase English letters.<br><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Check the first letter of each word<\/strong><\/h2>\n\n\n\n<p>No need to concatenate, just check the first letter of each word.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isAcronym(vector<string>& words, string_view s) {\n    if (words.size() != s.size()) return false;\n    for (int i = 0; i < s.size(); ++i)\n      if (words[i][0] != s[i]) return false;\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of strings&nbsp;words&nbsp;and a string&nbsp;s, determine if&nbsp;s&nbsp;is an&nbsp;acronym&nbsp;of words. The string&nbsp;s&nbsp;is considered an acronym of&nbsp;words&nbsp;if it can be formed by concatenating the&nbsp;first&nbsp;character of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,4],"class_list":["post-10082","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10082"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10082\/revisions"}],"predecessor-version":[{"id":10084,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10082\/revisions\/10084"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}