Ctrl K

Numbered Text Chunks

Hard
A texting service cuts a long text into pieces whose length is capped by width. Every piece gets a tag <a/b> appended at its end, where b is the total number of pieces and a is the piece's number, from 1 to b. The rules: - every piece, tag included, is exactly width characters long; only the last piece may be shorter (but never longer than width); - stripping the tags and joining the pieces in order gives back the original text; - there must be as few pieces as possible. Return the pieces in order. If the text cannot be cut under these rules, return an empty list.
Example 1
text='see you at noon', width=8 → ['see<1/5>', ' yo<2/5>', 'u a<3/5>', 't n<4/5>', 'oon<5/5>']

Each piece holds 3 characters of text plus a tag like <1/5>, so 15 characters make exactly 5 pieces. Four pieces would fit only 12 characters.

Example 2
text='hi', width=7 → ['hi<1/1>']

The whole text fits in a single piece: hi<1/1>, exactly 7 long.

Example 3
text='hello', width=5 → []

The tag <1/k> alone takes 5 characters or more, leaving no room for text, so no split exists.

Constraints
  • 1 ≤ len(text) ≤ 10⁴
  • text consists of lowercase English letters and spaces
  • 1 ≤ width ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNumbers From Working Keys HardMirror Squares in a Range HardLongest Repeated Fragment HardWhere Moss Species Meet HardPattern Woven Several Times Hard
loading Python…
Input
text='see you at noon', width=8
Expected
['see<1/5>', ' yo<2/5>', 'u a<3/5>', 't n<4/5>', 'oon<5/5>']

Run checks the first 3 tests in your browser. Submit checks all 72 tests on the server, hidden ones included.