Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Check Distances Between Same Letters

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2399" 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

You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26. Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25). In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored. Return true if s is a well-spaced string, otherwise return false. Example 1: Input: s = "abaccb", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: true Explanation: - 'a' appears at indices 0 and 2 so it satisfies distance[0] = 1. - 'b' appears at indices 1 and 5 so it satisfies distance[1] = 3. - 'c' appears at indices 3 and 4 so it satisfies distance[2] = 0. Note that distance[3] = 5, but since 'd' does not appear in s, it can be ignored. Return true because s is a well-spaced string. Example 2: Input: s = "aa", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: false Explanation: - 'a' appears at indices 0 and 1 so there are zero letters between them. Because distance[0] = 1, s is not a well-spaced string. Constraints: 2 <= s.length <= 52 s consists only of lowercase English letters. Each letter appears in s exactly twice. distance.length == 26 0 <= distance[i] <= 50

Explanation

Here's a breakdown of the solution:

  • Iterate through the string: For each character in the string, find its second occurrence.
  • Validate distance: Check if the distance between the two occurrences matches the corresponding value in the distance array.
  • Early exit: If any distance check fails, return False immediately. If all checks pass, return True.

  • Time & Space Complexity: O(n), where n is the length of the string, and O(1) due to the fixed size of the dictionary (at most 26 entries).

Code

    def is_well_spaced(s: str, distance: list[int]) -> bool:
    """
    Checks if a string is well-spaced according to the given distance array.

    Args:
        s: The input string.
        distance: The distance array.

    Returns:
        True if the string is well-spaced, False otherwise.
    """
    first_occurrence = {}
    for i, char in enumerate(s):
        if char in first_occurrence:
            expected_distance = distance[ord(char) - ord('a')]
            actual_distance = i - first_occurrence[char] - 1
            if actual_distance != expected_distance:
                return False
        else:
            first_occurrence[char] = i
    return True

More from this blog

C

Chatmagic blog

2894 posts