2068. Check Whether Two Strings are Almost Equivalent LeetCode Solution

In this guide, you will get 2068. Check Whether Two Strings are Almost Equivalent LeetCode Solution with the best time and space complexity. The solution to Check Whether Two Strings are Almost Equivalent problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check Whether Two Strings are Almost Equivalent solution in C++
  4. Check Whether Two Strings are Almost Equivalent solution in Java
  5. Check Whether Two Strings are Almost Equivalent solution in Python
  6. Additional Resources
2068. Check Whether Two Strings are Almost Equivalent LeetCode Solution image

Problem Statement of 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.

See also  956. Tallest Billboard LeetCode Solution

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.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

2068. Check Whether Two Strings are Almost Equivalent LeetCode Solution in C++

class Solution {
 public:
  bool checkAlmostEquivalent(string word1, string word2) {
    vector<int> count(26);

    for (const char c : word1)
      ++count[c - 'a'];

    for (const char c : word2)
      --count[c - 'a'];

    return ranges::all_of(count, [](int freq) { return abs(freq) <= 3; });
  }
};
/* code provided by PROGIEZ */

2068. Check Whether Two Strings are Almost Equivalent LeetCode Solution in Java

class Solution {
  public boolean checkAlmostEquivalent(String word1, String word2) {
    int[] count = new int[26];

    for (final char c : word1.toCharArray())
      ++count[c - 'a'];

    for (final char c : word2.toCharArray())
      --count[c - 'a'];

    return Arrays.stream(count).allMatch(freq -> Math.abs(freq) <= 3);
  }
}
// code provided by PROGIEZ

2068. Check Whether Two Strings are Almost Equivalent LeetCode Solution in Python

class Solution:
  def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
    count = collections.Counter(word1)
    count.subtract(collections.Counter(word2))
    return all(abs(freq) <= 3 for freq in count.values())
# code by PROGIEZ

Additional Resources

See also  2648. Generate Fibonacci Sequence LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.