Skip to main content
2 of 3
Copy edited.
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

If you want to do it quickly, you can use a two scan approach. Pseudo code:

  1. First parse. Find how many matching characters.
  2. Expand the length of the string.
  3. Second parse. Start from the end of the string. When we get a match we replace, else we just copy the chars from the first string.

I am not sure if this can be optimized to an in-place algorithm.

Damian
  • 4.7k
  • 6
  • 44
  • 71