Leetcode Problem 2109. Adding Spaces to a String

2109. Adding Spaces to a String

Leetcode Solutions

Iterative Space Insertion

  1. Initialize an empty string res to store the result.
  2. Initialize lastIdx to 0 to keep track of the last index processed in s.
  3. Iterate over each index i in spaces: a. Append the substring of s from lastIdx to i to res. b. Append a space to res. c. Update lastIdx to i.
  4. After the loop, append the remaining substring of s from lastIdx to the end to res.
  5. Return res.
UML Thumbnail

In-place Space Insertion with List Conversion

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...