Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Largest Substring Between Two Equal Characters

Updated
3 min read

Introduction

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

Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1. A substring is a contiguous sequence of characters within a string. Example 1: Input: s = "aa" Output: 0 Explanation: The optimal substring here is an empty substring between the two 'a's. Example 2: Input: s = "abca" Output: 2 Explanation: The optimal substring here is "bc". Example 3: Input: s = "cbzxy" Output: -1 Explanation: There are no characters that appear twice in s. Constraints: 1 <= s.length <= 300 s contains only lowercase English letters.

Explanation

Here's the solution:

  • First Occurrence Tracking: Iterate through the string and store the index of the first occurrence of each character in a dictionary (or array since it's lowercase English letters).
  • Length Calculation: Iterate through the string again. If the current character is present in the dictionary, calculate the length of the substring between the first occurrence (stored in the dictionary) and the current index, excluding the characters at the boundaries. Update the maximum length found so far.
  • No Match Handling: If no such substring is found (the dictionary remains empty, or the maximum length remains its initial value), return -1.

  • Runtime & Storage Complexity: O(n) time complexity and O(1) space complexity (due to the limited character set; an array of size 26 is used to store first occurrences).

Code

    def longest_substring_between_equal_characters(s: str) -> int:
    """
    Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.
    A substring is a contiguous sequence of characters within a string.

    Example 1:
    Input: s = "aa"
    Output: 0
    Explanation: The optimal substring here is an empty substring between the two 'a's.

    Example 2:
    Input: s = "abca"
    Output: 2
    Explanation: The optimal substring here is "bc".

    Example 3:
    Input: s = "cbzxy"
    Output: -1
    Explanation: There are no characters that appear twice in s.

    Constraints:
    1 <= s.length <= 300
    s contains only lowercase English letters.
    """
    first_occurrence = {}
    max_length = -1

    for i, char in enumerate(s):
        if char in first_occurrence:
            max_length = max(max_length, i - first_occurrence[char] - 1)
        else:
            first_occurrence[char] = i

    return max_length

More from this blog

C

Chatmagic blog

2894 posts