{x}
blog image

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:

Input: word1 = "aaaa", word2 = "bccb"
Output: false
Explanation: There are 4 'a's in "aaaa" but 0 'a's in "bccb".
The difference is 4, which is more than the allowed 3.

Example 2:

Input: word1 = "abcdeef", word2 = "abaaacc"
Output: true
Explanation: 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 is 3.
- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.
- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.
- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.
- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.
- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.

Example 3:

Input: word1 = "cccddabba", word2 = "babababab"
Output: true
Explanation: 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 is 2.
- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.
- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.
- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.

 

Constraints:

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

Solution Explanation: Check Whether Two Strings are Almost Equivalent

This problem asks whether two strings, word1 and word2, are "almost equivalent." Two strings are almost equivalent if the absolute difference in the frequency of each letter (a-z) between the two strings is at most 3.

Approach: Counting Character Frequencies

The most efficient approach involves using a counter (a hash map or array) to track the frequency of each character. Here's a breakdown:

  1. Initialization: Create a counter (we'll use an array of size 26 for simplicity, representing the 26 lowercase English letters). Initialize all counts to 0.

  2. Counting for word1: Iterate through word1. For each character, increment its corresponding count in the counter. We use the character's ASCII value minus the ASCII value of 'a' to get its index in the array (0 for 'a', 1 for 'b', etc.).

  3. Counting for word2: Iterate through word2. For each character, decrement its corresponding count in the counter.

  4. Checking the Differences: Iterate through the counter. If the absolute value of any count is greater than 3, it means the difference in frequency for that letter exceeds the allowed limit, so return false.

  5. Return true: If all counts have absolute values less than or equal to 3, the strings are almost equivalent, so return true.

Time and Space Complexity

  • Time Complexity: O(n), where n is the length of the strings. We iterate through each string once to count character frequencies and then iterate through the counter (which is constant size).

  • Space Complexity: O(1). The counter array has a fixed size of 26, regardless of the input string length.

Code Implementation (Python)

from collections import Counter
 
class Solution:
    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
        cnt = Counter(word1)  # Efficiently counts character frequencies in word1
        for char in word2:
            cnt[char] -= 1      # Subtract frequencies from word2
        return all(abs(count) <= 3 for count in cnt.values()) #Check if all differences are <=3
 

This Python code leverages the Counter object from the collections module for efficient frequency counting. The all() function concisely checks the condition for all character counts.

Code Implementation (Other Languages)

The same approach can be implemented in other languages using arrays or hash maps. The core logic remains the same: count frequencies, subtract, and check for differences within the limit. See the provided code snippets in Java, C++, Go, TypeScript, JavaScript, C#, and PHP for examples in those languages. They all follow the described algorithm, differing primarily in syntax and data structure specifics.