Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Number of Non-Overlapping Substrings

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1520" 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 of lowercase letters, you need to find the maximum number of non-empty substrings of s that meet the following conditions: The substrings do not overlap, that is for any two substrings s[i..j] and s[x..y], either j < x or i > y is true. A substring that contains a certain character c must also contain all occurrences of c. Find the maximum number of substrings that meet the above conditions. If there are multiple solutions with the same number of substrings, return the one with minimum total length. It can be shown that there exists a unique solution of minimum total length. Notice that you can return the substrings in any order. Example 1: Input: s = "adefaddaccc" Output: ["e","f","ccc"] Explanation: The following are all the possible substrings that meet the conditions: [ "adefaddaccc" "adefadda", "ef", "e", "f", "ccc", ] If we choose the first string, we cannot choose anything else and we'd get only 1. If we choose "adefadda", we are left with "ccc" which is the only one that doesn't overlap, thus obtaining 2 substrings. Notice also, that it's not optimal to choose "ef" since it can be split into two. Therefore, the optimal way is to choose ["e","f","ccc"] which gives us 3 substrings. No other solution of the same number of substrings exist. Example 2: Input: s = "abbaccd" Output: ["d","bb","cc"] Explanation: Notice that while the set of substrings ["d","abba","cc"] also has length 3, it's considered incorrect since it has larger total length. Constraints: 1 <= s.length <= 105 s contains only lowercase English letters.

Explanation

Here's the breakdown of the approach and the Python code:

  • Identify Character Ranges: Find the first and last occurrences of each character in the string. These ranges will define the potential substrings.
  • Merge Overlapping Ranges: Iterate through the characters and merge any overlapping ranges. This ensures that a substring containing a character includes all occurrences of that character, respecting the overlapping constraint.
  • Extract Non-Overlapping Substrings: After merging, iterate through the characters again, extracting valid non-overlapping substrings in a greedy manner. Sort the substrings by their ending indices. If the current character's range doesn't overlap with the previous substring's range, extract it as a new substring.

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

  • Storage Complexity: O(1), The alphabet size (26) is considered constant.

Code

    def max_num_of_substrings(s: str) -> list[str]:
    first = {}
    last = {}
    for i, c in enumerate(s):
        if c not in first:
            first[c] = i
        last[c] = i

    intervals = []
    for c in first:
        intervals.append([first[c], last[c], c])

    intervals.sort()

    def merge_intervals(intervals):
        res = []
        for start, end, c in intervals:
            if not res:
                res.append([start, end, c])
            else:
                prev_start, prev_end, prev_c = res[-1]
                if start <= prev_end:
                    new_start = min(start, prev_start)
                    new_end = max(end, prev_end)
                    new_c = "" # Doesn't matter the c
                    res[-1] = [new_start, new_end, new_c]
                else:
                    res.append([start, end, c])
        return res

    def validate(start, end):
      max_end = end
      for i in range(start, end + 1):
        char = s[i]
        if first[char] < start or last[char] > end:
          return -1, -1
      return start, end


    merged_intervals = []
    for c in first:
      start, end = first[c], last[c]
      new_start, new_end = validate(start, end)

      if new_start != -1:
        merged_intervals.append([new_start, new_end, c])

    merged_intervals.sort(key=lambda x: x[1])


    res = []
    last_end = -1
    for start, end, c in merged_intervals:
      if start > last_end:
          res.append(s[start:end+1])
          last_end = end

    return res

More from this blog

C

Chatmagic blog

2894 posts