Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Split Message Based on Limit

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2468" 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 string, message, and a positive integer, limit. You must split message into one or more parts based on limit. Each resulting part should have the suffix "", where "b" is to be replaced with the total number of parts and "a" is to be replaced with the index of the part, starting from 1 and going up to b. Additionally, the length of each resulting part (including its suffix) should be equal to limit, except for the last part whose length can be at most limit. The resulting parts should be formed such that when their suffixes are removed and they are all concatenated in order, they should be equal to message. Also, the result should contain as few parts as possible. Return the parts message would be split into as an array of strings. If it is impossible to split message as required, return an empty array. Example 1: Input: message = "this is really a very awesome message", limit = 9 Output: ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"] Explanation: The first 9 parts take 3 characters each from the beginning of message. The next 5 parts take 2 characters each to finish splitting message. In this example, each part, including the last, has length 9. It can be shown it is not possible to split message into less than 14 parts. Example 2: Input: message = "short message", limit = 15 Output: ["short mess<1/2>","age<2/2>"] Explanation: Under the given constraints, the string can be split into two parts: - The first part comprises of the first 10 characters, and has a length 15. - The next part comprises of the last 3 characters, and has a length 8. Constraints: 1 <= message.length <= 104 message consists only of lowercase English letters and ' '. 1 <= limit <= 104

Explanation

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

  • High-level approach:

    • Calculate the maximum possible number of parts. If even with this maximum, the suffix length is too great to fit, it's impossible to split the message.
    • Iteratively determine the actual number of parts. The key idea is to derive the length of the message portion in each part, considering the limit and suffix length.
    • Split the message into parts, constructing the suffix for each part.
  • Complexity:

    • Runtime Complexity: O(n), where n is the length of the message.
    • Storage Complexity: O(n), to store the resulting array of strings.

Code

    def splitMessage(message: str, limit: int) -> list[str]:
    n = len(message)
    max_parts = 1
    temp = 1
    while temp <= n:
        max_parts *= 10
        temp = len(str(max_parts))
    max_parts = max_parts // 10

    def suffix_length(parts):
        return len(f"<{1}/{parts}>")

    def can_split(parts):
        message_length_per_part = limit - suffix_length(parts)
        if message_length_per_part <= 0:
            return False

        total_length_possible = message_length_per_part * parts
        return total_length_possible >= n

    low = 1
    high = n

    parts = -1
    while low <= high:
        mid = (low + high) // 2
        if can_split(mid):
            parts = mid
            high = mid - 1
        else:
            low = mid + 1

    if parts == -1:
        return []

    message_length_per_part = limit - suffix_length(parts)
    if message_length_per_part <= 0:
        return []

    result = []
    start = 0
    for i in range(1, parts + 1):
        end = min(start + message_length_per_part, n)
        result.append(message[start:end] + f"<{i}/{parts}>")
        start = end

    return result

More from this blog

C

Chatmagic blog

2894 posts