Leetcode Problem 2068. Check Whether Two Strings are Almost Equivalent

2068. Check Whether Two Strings are Almost Equivalent

Leetcode Solutions

Key approach of the solution.

  1. Initialize an array cnt of size 26 to 0, representing the frequency difference for each letter from 'a' to 'z'.
  2. Iterate over the indices of the strings word1 and word2. a. Increment the count for the character in word1 at the current index. b. Decrement the count for the character in word2 at the same index.
  3. After the iteration, check each element in the cnt array. a. If any element's absolute value is greater than 3, return false.
  4. If none exceed 3, return true.
UML Thumbnail

Alternative approach using HashMap

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...