Leetcode 2068: Check Whether Two Strings are Almost Equivalent

grid47
grid47
Exploring patterns and algorithms
Apr 14, 2024 4 min read

You are given two strings, word1 and word2, each of length n. The two strings are considered almost equivalent if the difference in frequency of each letter between word1 and word2 is at most 3. Return true if they are almost equivalent, or false otherwise.
Problem
Approach
Steps
Complexity
Input: Two strings of equal length `n` are provided as input. Each string consists of lowercase English letters.
Example: word1 = "abcd", word2 = "abdc"
Constraints:
• 1 <= n <= 100
• Both word1 and word2 consist only of lowercase English letters.
Output: Return `true` if the two strings are almost equivalent, otherwise return `false`.
Example: true
Constraints:
Goal: Check if the absolute frequency difference for each letter in the two strings is at most 3.
Steps:
• Count the frequency of each letter in both strings.
• Calculate the absolute difference of the frequencies for each letter.
• If any difference exceeds 3, return false; otherwise, return true.
Goal: The length of the input strings will not exceed 100 characters, and they will only contain lowercase English letters.
Steps:
• 1 <= n <= 100
• word1 and word2 consist only of lowercase English letters.
Assumptions:
• Both input strings are of equal length.
Input: word1 = "abcd", word2 = "abdc"
Explanation: Both strings have identical characters, and the frequency difference for each letter is 0, which is within the allowed limit.

Input: word1 = "zzzz", word2 = "aaabb"
Explanation: The difference in the frequency of 'z' is 4, which exceeds the allowed limit of 3.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus