Original problem · C++ solution
Output the characters in original order and place one space between neighboring characters, with a newline after the last.
Output the characters in original order and place one space between neighboring characters, with a newline after the last.
- Time: O(n).
- Extra space: O(n).
std::string provides zero-based character access, while size() returns an unsigned count. Guard neighbor indices and use the string length when scanning its characters.
Preserve letter case and exact symbols; a character index and its one-based reported position differ by one.
State the invariant without looking at the code. Give one input that breaks the most tempting incorrect approach, then add it to the tests.