{"id":9880,"date":"2022-10-31T10:25:21","date_gmt":"2022-10-31T17:25:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9880"},"modified":"2022-10-31T10:26:50","modified_gmt":"2022-10-31T17:26:50","slug":"leetcode-2452-words-within-two-edits-of-dictionary%ef%bf%bc","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2452-words-within-two-edits-of-dictionary%ef%bf%bc\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2452.\u00a0Words Within Two Edits of Dictionary"},"content":{"rendered":"\n<p>You are given two string arrays,&nbsp;<code>queries<\/code>&nbsp;and&nbsp;<code>dictionary<\/code>. All words in each array comprise of lowercase English letters and have the same length.<\/p>\n\n\n\n<p>In one&nbsp;<strong>edit<\/strong>&nbsp;you can take a word from&nbsp;<code>queries<\/code>, and change any letter in it to any other letter. Find all words from&nbsp;<code>queries<\/code>&nbsp;that, after a&nbsp;<strong>maximum<\/strong>&nbsp;of two edits, equal some word from&nbsp;<code>dictionary<\/code>.<\/p>\n\n\n\n<p>Return<em>&nbsp;a list of all words from&nbsp;<\/em><code>queries<\/code><em>,&nbsp;<\/em><em>that match with some word from&nbsp;<\/em><code>dictionary<\/code><em>&nbsp;after a maximum of&nbsp;<strong>two edits<\/strong><\/em>. Return the words in the&nbsp;<strong>same order<\/strong>&nbsp;they appear in&nbsp;<code>queries<\/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> queries = [\"word\",\"note\",\"ants\",\"wood\"], dictionary = [\"wood\",\"joke\",\"moat\"]\n<strong>Output:<\/strong> [\"word\",\"note\",\"wood\"]\n<strong>Explanation:<\/strong>\n- Changing the 'r' in \"word\" to 'o' allows it to equal the dictionary word \"wood\".\n- Changing the 'n' to 'j' and the 't' to 'k' in \"note\" changes it to \"joke\".\n- It would take more than 2 edits for \"ants\" to equal a dictionary word.\n- \"wood\" can remain unchanged (0 edits) and match the corresponding dictionary word.\nThus, we return [\"word\",\"note\",\"wood\"].\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> queries = [\"yes\"], dictionary = [\"not\"]\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong>\nApplying any two edits to \"yes\" cannot make it equal to \"not\". Thus, we return an empty array.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= queries.length, dictionary.length &lt;= 100<\/code><\/li><li><code>n == queries[i].length == dictionary[j].length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li>All&nbsp;<code>queries[i]<\/code>&nbsp;and&nbsp;<code>dictionary[j]<\/code>&nbsp;are composed of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hamming distance + Brute Force<\/strong><\/h2>\n\n\n\n<p>For each query word q, check the hamming distance between it and all words in the dictionary.<\/p>\n\n\n\n<p>Time complexity: O(|q|*|d|*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  vector<string> twoEditWords(vector<string>& queries, vector<string>& dictionary) {\n    const int n = queries[0].size();\n    auto check = [&](string_view q) {      \n      for (const string& w : dictionary) {\n        int dist = 0;\n        for (int i = 0; i < n &#038;&#038; dist <= 3; ++i)\n          dist += q[i] != w[i];\n        if (dist <= 2) return true;\n      }\n      return false;\n    };\n    vector<string> ans;\n    for (const string& q : queries) \n      if (check(q)) ans.push_back(q);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two string arrays,&nbsp;queries&nbsp;and&nbsp;dictionary. All words in each array comprise of lowercase English letters and have the same length. In one&nbsp;edit&nbsp;you can take&#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":[787,188,177,4],"class_list":["post-9880","post","type-post","status-publish","format-standard","hentry","category-string","tag-edit-distance","tag-hamming-distance","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9880","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=9880"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9880\/revisions"}],"predecessor-version":[{"id":9883,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9880\/revisions\/9883"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}