Solved! Leetcode 2068. Check Whether Two Strings are Almost Equivalent

Description 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

Example 2

Example 3

Constraints

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

Solution

Time Complexity

O(n), where n is the length of words

Space Complexity

O(26 * 2) -> O(1), constant space

Rate this post

Leave a Reply