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 most3.
Given two strings word1 and word2, each of length n, return trueifword1andword2arealmost equivalent , orfalseotherwise.
The frequency of a letter x is the number of times it occurs in the string.
Input: word1 ="aaaa", word2 ="bccb"Output: falseExplanation: There are 4'a's in"aaaa" but 0'a's in"bccb".The difference is4, which is more than the allowed 3.
Input: word1 ="abcdeef", word2 ="abaaacc"Output: trueExplanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:-'a' appears 1 time in word1 and 4 times in word2. The difference is3.-'b' appears 1 time in word1 and 1 time in word2. The difference is0.-'c' appears 1 time in word1 and 2 times in word2. The difference is1.-'d' appears 1 time in word1 and 0 times in word2. The difference is1.-'e' appears 2 times in word1 and 0 times in word2. The difference is2.-'f' appears 1 time in word1 and 0 times in word2. The difference is1.
Input: word1 ="cccddabba", word2 ="babababab"Output: trueExplanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:-'a' appears 2 times in word1 and 4 times in word2. The difference is2.-'b' appears 2 times in word1 and 5 times in word2. The difference is3.-'c' appears 3 times in word1 and 0 times in word2. The difference is3.-'d' appears 2 times in word1 and 0 times in word2. The difference is2.
We need to compare the frequency of each letter in both strings and ensure the difference for every letter is at most 3. This is a direct frequency counting problem.
By counting the occurrences of each letter in both strings, we can compare the absolute difference for each letter. If all differences are at most 3, the strings are almost equivalent.