{"id":8690,"date":"2021-11-13T19:41:21","date_gmt":"2021-11-14T03:41:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8690"},"modified":"2021-11-13T19:42:50","modified_gmt":"2021-11-14T03:42:50","slug":"leetcode-2068-check-whether-two-strings-are-almost-equivalent","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2068-check-whether-two-strings-are-almost-equivalent\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2068. Check Whether Two Strings are Almost Equivalent"},"content":{"rendered":"\n<p>Two strings&nbsp;<code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>&nbsp;are considered&nbsp;<strong>almost equivalent<\/strong>&nbsp;if the differences between the frequencies of each letter from&nbsp;<code>'a'<\/code>&nbsp;to&nbsp;<code>'z'<\/code>&nbsp;between&nbsp;<code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>&nbsp;is&nbsp;<strong>at most<\/strong>&nbsp;<code>3<\/code>.<\/p>\n\n\n\n<p>Given two strings&nbsp;<code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>, each of length&nbsp;<code>n<\/code>, return&nbsp;<code>true<\/code>&nbsp;<em>if&nbsp;<\/em><code>word1<\/code>&nbsp;<em>and<\/em>&nbsp;<code>word2<\/code>&nbsp;<em>are&nbsp;<strong>almost equivalent<\/strong>, or<\/em>&nbsp;<code>false<\/code>&nbsp;<em>otherwise<\/em>.<\/p>\n\n\n\n<p>The&nbsp;<strong>frequency<\/strong>&nbsp;of a letter&nbsp;<code>x<\/code>&nbsp;is the number of times it occurs in the string.<\/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> word1 = \"aaaa\", word2 = \"bccb\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> There are 4 'a's in \"aaaa\" but 0 'a's in \"bccb\".\nThe difference is 4, which is more than the allowed 3.\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> word1 = \"abcdeef\", word2 = \"abaaacc\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The differences between the frequencies of each letter in word1 and word2 are at most 3:\n- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3.\n- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.\n- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.\n- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.\n- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.\n- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.\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> word1 = \"cccddabba\", word2 = \"babababab\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The differences between the frequencies of each letter in word1 and word2 are at most 3:\n- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2.\n- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.\n- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.\n- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == word1.length == word2.length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>&nbsp;consist only of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to track the <strong>relative frequency<\/strong> of a letter. <\/p>\n\n\n\n<p>Time complexity: O(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  bool checkAlmostEquivalent(string word1, string word2) {    \n    vector<int> m(26);\n    for (char c: word1) ++m[c - 'a'];\n    for (char c: word2) --m[c - 'a'];\n    for (int i = 0; i < 26; ++i)\n      if (abs(m[i]) > 3) return false;\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Two strings&nbsp;word1&nbsp;and&nbsp;word2&nbsp;are considered&nbsp;almost equivalent&nbsp;if the differences between the frequencies of each letter from&nbsp;&#8216;a&#8217;&nbsp;to&nbsp;&#8216;z&#8217;&nbsp;between&nbsp;word1&nbsp;and&nbsp;word2&nbsp;is&nbsp;at most&nbsp;3. Given two strings&nbsp;word1&nbsp;and&nbsp;word2, each of length&nbsp;n, return&nbsp;true&nbsp;if&nbsp;word1&nbsp;and&nbsp;word2&nbsp;are&nbsp;almost equivalent, or&nbsp;false&nbsp;otherwise. The&nbsp;frequency&nbsp;of a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,82,4],"class_list":["post-8690","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8690","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=8690"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8690\/revisions"}],"predecessor-version":[{"id":8692,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8690\/revisions\/8692"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}