{"id":3637,"date":"2018-08-19T20:29:33","date_gmt":"2018-08-20T03:29:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3637"},"modified":"2018-08-19T20:29:40","modified_gmt":"2018-08-20T03:29:40","slug":"leetcode-890-find-and-replace-pattern","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-890-find-and-replace-pattern\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 890. Find and Replace Pattern"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>You have a list of\u00a0<code>words<\/code>\u00a0and a\u00a0<code>pattern<\/code>, and you want to know which words in\u00a0<code>words<\/code>\u00a0matches the pattern.<\/p>\n<p>A word matches the pattern if there exists a permutation of letters\u00a0<code>p<\/code>\u00a0so that after replacing every letter\u00a0<code>x<\/code>\u00a0in the pattern with\u00a0<code>p(x)<\/code>, we get the desired word.<\/p>\n<p>(<em>Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.<\/em>)<\/p>\n<p>Return a list of the words in\u00a0<code>words<\/code>\u00a0that match the given pattern.<\/p>\n<p>You may return the answer in any order.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>words = <span id=\"example-input-1-1\">[\"abc\",\"deq\",\"mee\",\"aqq\",\"dkd\",\"ccc\"]<\/span>, pattern = <span id=\"example-input-1-2\">\"abb\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[\"mee\",\"aqq\"]<\/span>\r\n<strong>Explanation: <\/strong>\"mee\" matches the pattern because there is a permutation {a -&gt; m, b -&gt; e, ...}. \r\n\"ccc\" does not match the pattern because {a -&gt; c, b -&gt; c, ...} is not a permutation,\r\nsince a and b map to the same letter.<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= words.length &lt;= 50<\/code><\/li>\n<li><code>1 &lt;= pattern.length = words[i].length\u00a0&lt;= 20<\/code><\/li>\n<\/ul>\n<p><ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2404451723245401\"\n     data-ad-slot=\"7983117522\"><\/ins><\/p>\n<h1>Solution: Remember\u00a0the last pos of each char.<\/h1>\n<p>Time complexity: O(n*l)<\/p>\n<p>Space complexity: O(128) -&gt; O(26)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; findAndReplacePattern(vector&lt;string&gt;&amp; words, string pattern) {    \r\n    vector&lt;string&gt; ans;\r\n    for (const string&amp; w : words)\r\n      if (match(w, pattern)) ans.push_back(w);\r\n    return ans;\r\n  }\r\nprivate:\r\n  bool match(const string&amp; w, const string&amp; p) {\r\n    vector&lt;int&gt; last_pos_w(128, INT_MIN); \/\/ last pos of x in w\r\n    vector&lt;int&gt; last_pos_p(128, INT_MIN); \/\/ last pos of x in p\r\n    for (int i = 0; i &lt; w.size(); ++i)\r\n      if (last_pos_w[w[i]] != last_pos_p[p[i]]) {\r\n        return false;\r\n      } else {\r\n        last_pos_w[w[i]] = last_pos_p[p[i]] = i;\r\n      }\r\n    return true;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem You have a list of\u00a0words\u00a0and a\u00a0pattern, and you want to know which words in\u00a0words\u00a0matches the pattern. A word matches the pattern if there exists&#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":[177,315,4],"class_list":["post-3637","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-permuation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3637","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=3637"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3637\/revisions"}],"predecessor-version":[{"id":3639,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3637\/revisions\/3639"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}