{"id":9180,"date":"2021-12-20T18:22:10","date_gmt":"2021-12-21T02:22:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9180"},"modified":"2021-12-20T18:31:30","modified_gmt":"2021-12-21T02:31:30","slug":"leetcode-2108-find-first-palindromic-string-in-the-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2108-find-first-palindromic-string-in-the-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2108. Find First Palindromic String in the Array"},"content":{"rendered":"\n<p>Given an array of strings&nbsp;<code>words<\/code>, return&nbsp;<em>the first&nbsp;<strong>palindromic<\/strong>&nbsp;string in the array<\/em>. If there is no such string, return&nbsp;<em>an&nbsp;<strong>empty string<\/strong>&nbsp;<\/em><code>\"\"<\/code>.<\/p>\n\n\n\n<p>A string is&nbsp;<strong>palindromic<\/strong>&nbsp;if it reads the same forward and backward.<\/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 = [\"abc\",\"car\",\"ada\",\"racecar\",\"cool\"]\n<strong>Output:<\/strong> \"ada\"\n<strong>Explanation:<\/strong> The first string that is palindromic is \"ada\".\nNote that \"racecar\" is also palindromic, but it is not the first.\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 = [\"notapalindrome\",\"racecar\"]\n<strong>Output:<\/strong> \"racecar\"\n<strong>Explanation:<\/strong> The first and only string that is palindromic is \"racecar\".\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 = [\"def\",\"ghi\"]\n<strong>Output:<\/strong> \"\"\n<strong>Explanation:<\/strong> There are no palindromic strings, so the empty string is returned.\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;= 100<\/code><\/li><li><code>words[i]<\/code>&nbsp;consists only of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Enumerate each word and check whether it&#8217;s a palindrome or not.<\/p>\n\n\n\n<p>Time complexity: O(n * l)<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  string firstPalindrome(vector<string>& words) {\n    auto isPalindrome = [](string_view s) {\n      size_t l = s.size();\n      for (size_t i = 0; i < l \/ 2; ++i)\n        if (s[i] != s[l - i - 1]) return false;\n      return true;\n    };\n    for (const string&#038; word : words)\n      if (isPalindrome(word)) return word;\n    return \"\";\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of strings&nbsp;words, return&nbsp;the first&nbsp;palindromic&nbsp;string in the array. If there is no such string, return&nbsp;an&nbsp;empty string&nbsp;&#8220;&#8221;. A string is&nbsp;palindromic&nbsp;if it reads the same&#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,95,4],"class_list":["post-9180","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-palindrome","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9180","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=9180"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9180\/revisions"}],"predecessor-version":[{"id":9183,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9180\/revisions\/9183"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}