Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Create a new string, initially empty, as the modified string. Iterate through the original string and append each character of the original string to the new string. However, each time you reach a character that requires a space before it, append a space before appending the character.
Since the array of indices for the space locations is sorted, use a pointer to keep track of the next index to place a space. Only increment the pointer once a space has been appended.
Ensure that your append operation can be done in O(1).