Split Message Based on Limit - Problem

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.

Input & Output

Example 1 — Basic Split
$ Input: message = "this is really a long text", limit = 16
Output: ["this is rea<1/3>", "lly a long <2/3>", "text<3/3>"]
💡 Note: Split into 3 parts: Part 1 has 11 chars + 5 char suffix = 16 total. Part 2 has 11 chars + 5 char suffix = 16 total. Part 3 has 4 chars + 5 char suffix = 9 total (≤ 16).
Example 2 — Impossible Split
$ Input: message = "short", limit = 15
Output: ["short<1/1>"]
💡 Note: Single part: "short" (5 chars) + "<1/1>" (5 chars) = 10 total ≤ 15, so this is possible.
Example 3 — Edge Case Single Part
$ Input: message = "ab", limit = 8
Output: ["ab<1/1>"]
💡 Note: Single part: "ab" (2 chars) + "<1/1>" (5 chars) = 7 chars ≤ 8 limit.

Constraints

  • 1 ≤ message.length ≤ 104
  • 1 ≤ limit ≤ 104

Visualization

Tap to expand
Split Message Based on Limit INPUT message: "this is really a long text" 26 characters total t h i s ... limit = 16 Max 16 chars per part Suffix format: <a/b> Constraints: - Each part <= limit chars - Include suffix in count - Minimize number of parts ALGORITHM STEPS 1 Binary Search Parts Find min parts needed 2 Calculate Suffix Length For b parts: len("<a/b>") b=3: suffix = "<1/3>" = 5 chars Available: 16 - 5 = 11 chars 3 Verify Capacity Check: parts * avail >= msg 3 * 11 = 33 >= 26 [OK] 3 parts is sufficient! 4 Build Result Parts Split and add suffixes chars[0-9] + "<1/3>" --> part1 chars[10-20] + "<2/3>" --> part2 chars[21-25] + "<3/3>" --> part3 FINAL RESULT Output Array (3 parts): Part 1 (16 chars): "this is re<1/3>" Part 2 (16 chars): "ally a long<2/3>" Part 3 (10 chars): " text<3/3>" Verification: "this is re" + "ally a long" + " text" = "this is really a long text" [OK] Match! Key Insight: The suffix length depends on the total number of parts (b). For single-digit b, suffix is 5 chars "<a/b>". Use binary search to find minimum parts where: (parts * available_chars) >= message_length. Handle multi-digit part numbers carefully - suffix length increases with more parts (e.g., "<10/99>" = 7 chars). TutorialsPoint - Split Message Based on Limit | Optimal Solution
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
12.4K Views
Medium Frequency
~35 min Avg. Time
285 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen