{"id":6594,"date":"2020-04-11T21:51:55","date_gmt":"2020-04-12T04:51:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6594"},"modified":"2020-04-11T21:53:18","modified_gmt":"2020-04-12T04:53:18","slug":"leetcode-1408-string-matching-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1408-string-matching-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1408. String Matching in an Array"},"content":{"rendered":"\n<p>Given an array of string&nbsp;<code>words<\/code>. Return all strings in&nbsp;<code>words<\/code>&nbsp;which is substring of another word in&nbsp;<strong>any<\/strong>&nbsp;order.&nbsp;<\/p>\n\n\n\n<p>String&nbsp;<code>words[i]<\/code>&nbsp;is substring of&nbsp;<code>words[j]<\/code>,&nbsp;if&nbsp;can be obtained removing some characters to left and\/or right side of&nbsp;<code>words[j]<\/code>.<\/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 = [\"mass\",\"as\",\"hero\",\"superhero\"]\n<strong>Output:<\/strong> [\"as\",\"hero\"]\n<strong>Explanation:<\/strong> \"as\" is substring of \"mass\" and \"hero\" is substring of \"superhero\".\n[\"hero\",\"as\"] is also a valid answer.\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 = [\"leetcode\",\"et\",\"code\"]\n<strong>Output:<\/strong> [\"et\",\"code\"]\n<strong>Explanation:<\/strong> \"et\", \"code\" are substring of \"leetcode\".\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 = [\"blue\",\"green\",\"bu\"]\n<strong>Output:<\/strong> []\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;= 30<\/code><\/li><li><code>words[i]<\/code>&nbsp;contains only lowercase English letters.<\/li><li>It&#8217;s&nbsp;<strong>guaranteed<\/strong>&nbsp;that&nbsp;<code>words[i]<\/code>&nbsp;will be unique.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<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  vector<string> stringMatching(vector<string>& words) {\n    vector<string> ans;\n    const int n = words.size();\n    for (int i = 0; i < n; ++i)\n      for (int j = 0; j < n; ++j) {\n        if (i == j) continue;\n        if (words[j].find(words[i]) != string::npos) {\n          ans.push_back(words[i]);\n          break;\n        }\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of string&nbsp;words. Return all strings in&nbsp;words&nbsp;which is substring of another word in&nbsp;any&nbsp;order.&nbsp; String&nbsp;words[i]&nbsp;is substring of&nbsp;words[j],&nbsp;if&nbsp;can be obtained removing some characters to left&#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-6594","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\/6594","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=6594"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6594\/revisions"}],"predecessor-version":[{"id":6596,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6594\/revisions\/6596"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}