|
| 1 | +# Check Whether Two Strings are Almost Equivalent |
| 2 | + |
| 3 | +**Link to Problem**: https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +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`. |
| 8 | + |
| 9 | +Given two strings `word1` and `word2`, each of length `n`, return `true` if `word1` and `word2` are **almost equivalent**, or `false` otherwise. |
| 10 | + |
| 11 | +The frequency of a letter `x` is the number of times it occurs in the string. |
| 12 | + |
| 13 | +## Examples |
| 14 | + |
| 15 | +### Example 1 |
| 16 | + |
| 17 | +``` |
| 18 | +Input: word1 = "aaaa", word2 = "bccb" |
| 19 | +Output: false |
| 20 | +Explanation: There are 4 'a's in "aaaa" but 0 'a's in "bccb". |
| 21 | +The difference is 4, which is more than the allowed 3. |
| 22 | +``` |
| 23 | + |
| 24 | +### Example 2 |
| 25 | + |
| 26 | +``` |
| 27 | +Input: word1 = "abcdeef", word2 = "abaaacc" |
| 28 | +Output: true |
| 29 | +Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3: |
| 30 | +- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3. |
| 31 | +- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0. |
| 32 | +- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1. |
| 33 | +- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1. |
| 34 | +- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2. |
| 35 | +- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1. |
| 36 | +``` |
| 37 | + |
| 38 | +### Example 3 |
| 39 | + |
| 40 | +``` |
| 41 | +Input: word1 = "cccddabba", word2 = "babababab" |
| 42 | +Output: true |
| 43 | +Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3: |
| 44 | +- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2. |
| 45 | +- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3. |
| 46 | +- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3. |
| 47 | +- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2. |
| 48 | +``` |
| 49 | + |
| 50 | +## Thoughts |
| 51 | + |
| 52 | +Initially thought I should use `Enum.frequencies()` on both words and compare, but then I would |
| 53 | +have to do another loop to compare the difference between each hash maps, so I didn't go for it. |
| 54 | + |
| 55 | +I ended up just initializing a blank hash map and looping through each word to count the occurrences |
| 56 | +of each character. The loop on the first word will count positively on each character and the loop |
| 57 | +on the second word will count negatively on each character. |
| 58 | + |
| 59 | +Once we get a resulting hash map from counting both words, we will filter the hash map to remove |
| 60 | +any frequencies that are below `4` and return `false` if the filtered hash map still has data |
| 61 | +inside it and `true` if otherwise. |
0 commit comments