|
| 1 | +2068\. Check Whether Two Strings are Almost Equivalent |
| 2 | + |
| 3 | +Easy |
| 4 | + |
| 5 | +Two strings `word1` and `word2` are considered **almost equivalent** if the differences between the frequencies of each letter from `'a'` to `'z'` between `word1` and `word2` is **at most** `3`. |
| 6 | + |
| 7 | +Given two strings `word1` and `word2`, each of length `n`, return `true` _if_ `word1` _and_ `word2` _are **almost equivalent**, or_ `false` _otherwise_. |
| 8 | + |
| 9 | +The **frequency** of a letter `x` is the number of times it occurs in the string. |
| 10 | + |
| 11 | +**Example 1:** |
| 12 | + |
| 13 | +**Input:** word1 = "aaaa", word2 = "bccb" |
| 14 | + |
| 15 | +**Output:** false |
| 16 | + |
| 17 | +**Explanation:** There are 4 'a's in "aaaa" but 0 'a's in "bccb". |
| 18 | + |
| 19 | +The difference is 4, which is more than the allowed 3. |
| 20 | + |
| 21 | +**Example 2:** |
| 22 | + |
| 23 | +**Input:** word1 = "abcdeef", word2 = "abaaacc" |
| 24 | + |
| 25 | +**Output:** true |
| 26 | + |
| 27 | +**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3: |
| 28 | + |
| 29 | +- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3. |
| 30 | + |
| 31 | +- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0. |
| 32 | + |
| 33 | +- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1. |
| 34 | + |
| 35 | +- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1. |
| 36 | + |
| 37 | +- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2. |
| 38 | + |
| 39 | +- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1. |
| 40 | + |
| 41 | +**Example 3:** |
| 42 | + |
| 43 | +**Input:** word1 = "cccddabba", word2 = "babababab" |
| 44 | + |
| 45 | +**Output:** true |
| 46 | + |
| 47 | +**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3: |
| 48 | + |
| 49 | +- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2. |
| 50 | + |
| 51 | +- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3. |
| 52 | + |
| 53 | +- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3. |
| 54 | + |
| 55 | +- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2. |
| 56 | + |
| 57 | +**Constraints:** |
| 58 | + |
| 59 | +* `n == word1.length == word2.length` |
| 60 | +* `1 <= n <= 100` |
| 61 | +* `word1` and `word2` consist only of lowercase English letters. |
0 commit comments