Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Check Whether Two Strings are Almost Equivalent

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2068" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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.

Explanation

  • Frequency Counting: Use an array (or dictionary) to store the frequency of each character in both strings. Iterate through each string and update the frequency counts accordingly.
    • Difference Check: Iterate through the frequency array, calculating the absolute difference in frequencies for each character between the two strings.
    • Early Exit: If at any point, the absolute difference exceeds 3, return false immediately. Otherwise, if the loop completes, return true.
  • Runtime Complexity: O(n), where n is the length of the strings.
  • Storage Complexity: O(1), as the frequency array has a fixed size of 26, irrespective of the input string length.

Code

    def almostEquivalent(word1: str, word2: str) -> bool:    """
    Checks if two strings are almost equivalent based on character frequencies.

    Args:
        word1: The first string.
        word2: The second string.

    Returns:
        True if the strings are almost equivalent, False otherwise.
    """

    freq = [0] * 26  # Frequency array for 'a' to 'z'

    # Count frequencies in word1
    for char in word1:
        freq[ord(char) - ord('a')] += 1

    # Subtract frequencies from word2
    for char in word2:
        freq[ord(char) - ord('a')] -= 1

    # Check if absolute differences are at most 3
    for count in freq:
        if abs(count) > 3:
            return False

    return True

More from this blog

C

Chatmagic blog

2894 posts