<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>edit distance Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/edit-distance/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/edit-distance/</link>
	<description></description>
	<lastBuildDate>Mon, 31 Oct 2022 17:26:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>edit distance Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/edit-distance/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2452. Words Within Two Edits of Dictionary</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2452-words-within-two-edits-of-dictionary%ef%bf%bc/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2452-words-within-two-edits-of-dictionary%ef%bf%bc/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 31 Oct 2022 17:25:21 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[edit distance]]></category>
		<category><![CDATA[hamming distance]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9880</guid>

					<description><![CDATA[<p>You are given two string arrays,&#160;queries&#160;and&#160;dictionary. All words in each array comprise of lowercase English letters and have the same length. In one&#160;edit&#160;you can take&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2452-words-within-two-edits-of-dictionary%ef%bf%bc/">花花酱 LeetCode 2452. Words Within Two Edits of Dictionary</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<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>



<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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]
<strong>Output:</strong> ["word","note","wood"]
<strong>Explanation:</strong>
- Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood".
- Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke".
- It would take more than 2 edits for "ants" to equal a dictionary word.
- "wood" can remain unchanged (0 edits) and match the corresponding dictionary word.
Thus, we return ["word","note","wood"].
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queries = ["yes"], dictionary = ["not"]
<strong>Output:</strong> []
<strong>Explanation:</strong>
Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Hamming distance + Brute Force</strong></h2>



<p>For each query word q, check the hamming distance between it and all words in the dictionary.</p>



<p>Time complexity: O(|q|*|d|*n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;string&gt; twoEditWords(vector&lt;string&gt;&amp; queries, vector&lt;string&gt;&amp; dictionary) {
    const int n = queries[0].size();
    auto check = [&amp;](string_view q) {      
      for (const string&amp; w : dictionary) {
        int dist = 0;
        for (int i = 0; i &lt; n &amp;&amp; dist &lt;= 3; ++i)
          dist += q[i] != w[i];
        if (dist &lt;= 2) return true;
      }
      return false;
    };
    vector&lt;string&gt; ans;
    for (const string&amp; q : queries) 
      if (check(q)) ans.push_back(q);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2452-words-within-two-edits-of-dictionary%ef%bf%bc/">花花酱 LeetCode 2452. Words Within Two Edits of Dictionary</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-2452-words-within-two-edits-of-dictionary%ef%bf%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
