Split Message Based on Limit - Problem
Message Pagination Problem: You need to split a message into multiple parts where each part includes pagination information in the format <a/b>.

Given a message string and a positive integer limit, you must:
  • Split the message into one or more parts
  • Each part must have suffix <a/b> where b is total parts and a is the current part number (1-indexed)
  • Each part's total length (including suffix) must be exactly limit, except the last part which can be โ‰ค limit
  • When suffixes are removed and parts concatenated, they should equal the original message
  • Minimize the total number of parts

Return the array of paginated parts, or an empty array if impossible.

Input & Output

example_1.py โ€” Basic Split
$ 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>", "ess<12/14>", "ag<13/14>", "e<14/14>"]
๐Ÿ’ก Note: With limit=9, each part can hold at most 9 characters including the suffix. The suffix "<10/14>" takes 7 chars, leaving 2 chars for content in parts 10-14. The algorithm finds that 14 parts is the minimum needed.
example_2.py โ€” Impossible Case
$ Input: message = "short message", limit = 15
โ€บ Output: []
๐Ÿ’ก Note: Even with 1 part, "short message<1/1>" would be 19 characters, exceeding the limit of 15. Since it's impossible to fit the message with any number of parts, return empty array.
example_3.py โ€” Single Part
$ Input: message = "hello", limit = 12
โ€บ Output: ["hello<1/1>"]
๐Ÿ’ก Note: The entire message "hello" plus suffix "<1/1>" equals "hello<1/1>" with length 10, which fits within limit 12. Only one part is needed.

Visualization

Tap to expand
Message: "this is really a very awesome message"Part 1"thi""<1/14>"Part 2"s i""<2/14>"Part 3"s r""<3/14>"...Part 13"ag""<13/14>"Part 14 (Last)"e""<14/14>"Key ConstraintEach part โ‰ค 9 characters(including suffix)Binary Search: Try 1, 2, 4, 8... parts until feasibleFound: 14 parts is minimum needed!
Understanding the Visualization
1
Calculate Space
For each candidate part count, calculate how much space suffixes will take
2
Check Feasibility
Verify if remaining space can accommodate the entire message
3
Binary Search
Use binary search to efficiently find the minimum feasible part count
4
Generate Result
Split the message using the optimal part count
Key Takeaway
๐ŸŽฏ Key Insight: Binary search works because if we can split into k parts, we can usually split into k+1 parts too (monotonic property). The challenge is calculating space efficiently for each candidate part count.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

We try up to n different part counts, and for each we simulate the splitting process which takes O(n) time

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space needed to store the result array and temporary calculations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค message.length โ‰ค 104
  • 1 โ‰ค limit โ‰ค 104
  • message consists only of lowercase English letters and spaces
  • 1 โ‰ค limit โ‰ค 104
Asked in
Google 32 Meta 28 Amazon 21 Microsoft 15
22.1K Views
Medium Frequency
~25 min Avg. Time
847 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