|
| 1 | +<h2><a href="https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent/?envType=problem-list-v2&envId=string">2068. Check Whether Two Strings are Almost Equivalent</a></h2><h3>Easy</h3><hr><p>Two strings <code>word1</code> and <code>word2</code> are considered <strong>almost equivalent</strong> if the differences between the frequencies of each letter from <code>'a'</code> to <code>'z'</code> between <code>word1</code> and <code>word2</code> is <strong>at most</strong> <code>3</code>.</p> |
| 2 | + |
| 3 | +<p>Given two strings <code>word1</code> and <code>word2</code>, each of length <code>n</code>, return <code>true</code> <em>if </em><code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>almost equivalent</strong>, or</em> <code>false</code> <em>otherwise</em>.</p> |
| 4 | + |
| 5 | +<p>The <strong>frequency</strong> of a letter <code>x</code> is the number of times it occurs in the string.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> word1 = "aaaa", word2 = "bccb" |
| 12 | +<strong>Output:</strong> false |
| 13 | +<strong>Explanation:</strong> There are 4 'a's in "aaaa" but 0 'a's in "bccb". |
| 14 | +The difference is 4, which is more than the allowed 3. |
| 15 | +</pre> |
| 16 | + |
| 17 | +<p><strong class="example">Example 2:</strong></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | +<strong>Input:</strong> word1 = "abcdeef", word2 = "abaaacc" |
| 21 | +<strong>Output:</strong> true |
| 22 | +<strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: |
| 23 | +- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3. |
| 24 | +- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0. |
| 25 | +- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1. |
| 26 | +- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1. |
| 27 | +- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2. |
| 28 | +- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong class="example">Example 3:</strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> word1 = "cccddabba", word2 = "babababab" |
| 35 | +<strong>Output:</strong> true |
| 36 | +<strong>Explanation:</strong> The differences between the frequencies of each letter in word1 and word2 are at most 3: |
| 37 | +- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2. |
| 38 | +- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3. |
| 39 | +- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3. |
| 40 | +- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>n == word1.length == word2.length</code></li> |
| 48 | + <li><code>1 <= n <= 100</code></li> |
| 49 | + <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> |
| 50 | +</ul> |
0 commit comments