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 "<a/b>", 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
Inputmessage = "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>"]
The split uses 14 parts, with the first 9 parts taking 3 characters each and the next 5 parts taking 2 characters each, and it is not possible to split the message into fewer parts.
Example 2
Inputmessage = "short message", limit = 15
Output["short mess<1/2>","age<2/2>"]
The first part contains the first 10 characters and has length 15, while the second part contains the last 3 characters and has length 8.

Constraints

  • 1 <= message.length <= 10^4
  • message consists only of lowercase English letters and ' '.
  • 1 <= limit <= 10^4

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate