Ctrl K

Split Text at the Marks

Medium
Some text got glued together without spaces. You are given a string text and a strictly increasing list of indices marks (0-based). Insert a single space before every character whose index appears in marks. The indices refer to the original string, not to the string after insertions. Return the resulting string.
Example 1
text='GoodMorningWorld', marks=[4, 11] → 'Good Morning World'

The character at index 4 is M and the one at index 11 is W; the spaces go right before them.

Example 2
text='abc', marks=[0, 2] → ' ab c'

A space before index 0 lands at the very start of the string.

Example 3
text='Hello', marks=[] → 'Hello'

No marks, so the string stays as it is.

Constraints
  • 1 ≤ len(text) ≤ 3 · 10⁵
  • text consists of lowercase and uppercase English letters
  • 0 ≤ len(marks) ≤ 3 · 10⁵
  • 0 ≤ marks[i] ≤ len(text) - 1
  • marks is strictly increasing
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Subsequence of One Word Only MediumLongest Word by Crossing Out MediumMatch-Three Cascade MediumWords With Drawn-Out Letters MediumCapitals Follow the Pattern Medium
loading Python…
Input
text='GoodMorningWorld', marks=[4, 11]
Expected
'Good Morning World'

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