Check Whether Two Strings are Almost Equivalent

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.

Given two strings word1 and word2, each of length n, return true if word1 and word2 are almost equivalent, or false otherwise.

The frequency of a letter x is the number of times it occurs in the string.

Example 1
Inputword1 = "aaaa", word2 = "bccb"
Outputfalse
There are 4 'a's in "aaaa" but 0 'a's in "bccb", so the difference is 4, which is more than the allowed 3.
Example 2
Inputword1 = "abcdeef", word2 = "abaaacc"
Outputtrue
For every letter, the difference between its frequency in word1 and word2 is at most 3.

Constraints

  • n == word1.length == word2.length
  • 1 <= n <= 100
  • word1 and word2 consist only of lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate