# Leetcode - Replace space

Note:

• Normal approach is pretty intuitive so we don’t talk about it.
• For double pointers:
• If there are n spaces in the string, the result will have 2n more chars than the original.
• Iterate string to find the count of spaces.
• Append 2n spaces to the stirng.
• Convert it to array so we can mutate it. (String is immutable in JS)
• Left pointer should be put at the end of the original string, and right is put at the end of the new string.
• When arr[left] === ' ', replace arr[right], arr[right-1], arr[right-2] with %20.
• Otherwise just arr[right--] = arr[left--]. (We don’t even need to swap them because old chars will be covered by new chars.)

Question:
Repalce every space in a string with %20

Example:

Normal approach
Code:

Double pointers (Save memory space)