2468. Split Message Based on Limit LeetCode Solution

In this guide, you will get 2468. Split Message Based on Limit LeetCode Solution with the best time and space complexity. The solution to Split Message Based on Limit problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Split Message Based on Limit solution in C++
  4. Split Message Based on Limit solution in Java
  5. Split Message Based on Limit solution in Python
  6. Additional Resources
2468. Split Message Based on Limit LeetCode Solution image

Problem Statement of Split Message Based on Limit

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.

See also  3417. Zigzag Grid Traversal With Skip LeetCode Solution

Example 1:

Input: message = “this is really a very awesome message”, limit = 9
Output: [“thi”,”s i”,”s r”,”eal”,”ly “,”a v”,”ery”,” aw”,”eso”,”me”,” m”,”es”,”sa”,”ge”]
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”,”age”]
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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2468. Split Message Based on Limit LeetCode Solution in C++

class Solution {
 public:
  vector<string> splitMessage(string message, int limit) {
    const int kMessageLength = message.length();
    int b = 1;
    // the total length of a: initialized with the length of "1"
    int aLength = sz(1);

    // the total length of b := b * sz(b)
    // The total length of "</>" := b * 3
    while (b * limit < b * (sz(b) + 3) + aLength + kMessageLength) {
      // If the length of the last suffix "<b/b>" := sz(b) * 2 + 3 >= limit,
      // then it's impossible that the length of "*<b/b>" <= limit.
      if (sz(b) * 2 + 3 >= limit)
        return {};
      aLength += sz(++b);
    }

    vector<string> ans;

    for (int i = 0, a = 1; a <= b; ++a) {
      // the length of "<a/b>" := sz(a) + sz(b) + 3
      const int j = limit - (sz(a) + sz(b) + 3);
      ans.push_back(message.substr(i, j) + "<" + to_string(a) + "/" +
                    to_string(b) + ">");
      i += j;
    }

    return ans;
  }

 private:
  int sz(int num) {
    return to_string(num).length();
  }
};
/* code provided by PROGIEZ */

2468. Split Message Based on Limit LeetCode Solution in Java

class Solution {
  public String[] splitMessage(String message, int limit) {
    final int kMessageLength = message.length();
    int b = 1;
    // the total length of a: initialized with the length of "1"
    int aLength = sz(1);

    // the total length of b := b * sz(b)
    // The total length of "</>" := b * 3
    while (b * limit < b * (sz(b) + 3) + aLength + kMessageLength) {
      // If the length of the last suffix "<b/b>" := sz(b) * 2 + 3 >= limit,
      // then it's impossible that the length of "*<b/b>" <= limit.
      if (sz(b) * 2 + 3 >= limit)
        return new String[] {};
      aLength += sz(++b);
    }

    String[] ans = new String[b];

    for (int i = 0, a = 1; a <= b; ++a) {
      // the length of "<a/b>" := sz(a) + sz(b) + 3
      final int j = limit - (sz(a) + sz(b) + 3);
      ans[a - 1] = message.substring(i, Math.min(message.length(), i + j)) + "<" +
                   String.valueOf(a) + "/" + String.valueOf(b) + ">";
      i += j;
    }

    return ans;
  }

  private int sz(int num) {
    return String.valueOf(num).length();
  }
}
// code provided by PROGIEZ

2468. Split Message Based on Limit LeetCode Solution in Python

class Solution:
  def splitMessage(self, message: str, limit: int) -> list[str]:
    kMessageLength = len(message)

    def sz(num: int):
      return len(str(num))

    b = 1
    # the total length of a: initialized with the length of "1"
    aLength = sz(1)

    # the total length of b := b * sz(b)
    # The total length of "</>" := b * 3
    while b * limit < b * (sz(b) + 3) + aLength + kMessageLength:
      # If the length of the last suffix "<b/b>" := sz(b) * 2 + 3 >= limit,
      # then it's impossible that the length of "*<b/b>" <= limit.
      if sz(b) * 2 + 3 >= limit:
        return []
      b += 1
      aLength += sz(b)

    ans = []

    i = 0
    for a in range(1, b + 1):
      # the length of "<a/b>" := sz(a) + sz(b) + 3
      j = limit - (sz(a) + sz(b) + 3)
      ans.append(f'{message[i:i + j]}<{a}/{b}>')
      i += j

    return ans
# code by PROGIEZ

Additional Resources

See also  865. Smallest Subtree with all the Deepest Nodes LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.