Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Minimize String Length

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2716" 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, you have two types of operation: Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if exists). Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the right of i (if exists). Your task is to minimize the length of s by performing the above operations zero or more times. Return an integer denoting the length of the minimized string. Example 1: Input: s = "aaabc" Output: 3 Explanation: Operation 2: we choose i = 1 so c is 'a', then we remove s[2] as it is closest 'a' character to the right of s[1]. s becomes "aabc" after this. Operation 1: we choose i = 1 so c is 'a', then we remove s[0] as it is closest 'a' character to the left of s[1]. s becomes "abc" after this. Example 2: Input: s = "cbbd" Output: 3 Explanation: Operation 1: we choose i = 2 so c is 'b', then we remove s[1] as it is closest 'b' character to the left of s[1]. s becomes "cbd" after this. Example 3: Input: s = "baadccab" Output: 4 Explanation: Operation 1: we choose i = 6 so c is 'a', then we remove s[2] as it is closest 'a' character to the left of s[6]. s becomes "badccab" after this. Operation 2: we choose i = 0 so c is 'b', then we remove s[6] as it is closest 'b' character to the right of s[0]. s becomes "badcca" fter this. Operation 2: we choose i = 3 so c is 'c', then we remove s[4] as it is closest 'c' character to the right of s[3]. s becomes "badca" after this. Operation 1: we choose i = 4 so c is 'a', then we remove s[1] as it is closest 'a' character to the left of s[4]. s becomes "bdca" after this. Constraints: 1 <= s.length <= 100 s contains only lowercase English letters

Explanation

Here's the breakdown of the solution:

  • Identify Unique Characters: Find the unique characters present in the input string. These characters are the only ones that can potentially remain in the minimized string.
  • First and Last Occurrence: For each unique character, find its first and last index in the original string.
  • Extract Substring: The minimized string will consist of all characters located between the first occurrence of the leftmost unique character and the last occurrence of the rightmost unique character.

  • Runtime Complexity: O(n), where n is the length of the string.

  • Storage Complexity: O(1). The storage required is independent of the string length as it only stores a fixed number of characters and their positions.

Code

    def solve():
    s = input()
    n = len(s)

    if not s:
        return 0

    unique_chars = set(s)

    first_occurrence = {}
    last_occurrence = {}

    for char in unique_chars:
        first_occurrence[char] = -1
        last_occurrence[char] = -1

    for i in range(n):
        if s[i] in unique_chars:
            if first_occurrence[s[i]] == -1:
                first_occurrence[s[i]] = i
            last_occurrence[s[i]] = i

    min_index = n
    max_index = -1

    for char in unique_chars:
        min_index = min(min_index, first_occurrence[char])
        max_index = max(max_index, last_occurrence[char])


    return max_index - min_index + 1

print(solve())

More from this blog

C

Chatmagic blog

2894 posts