Blogger

Delete comment from: Javarevisited

javin paul said...

Hello Aleksandar, as I have explained in the article, the in place algorithms are the ones which doesn't need to allocate extra memory, barring one or two intermediate variables. It's much more clear when you first solve reverse an array in place. So simplest way to just convert the string to character array and reverse.

Now, you may know that String is Immutable in Java and any mutation will create new string for example taking out one character from beginning and putting into end, hence we are using StringBuffer.

The use of StringBuffer here is not to allocate extra memory but facilitate swapping of character which makes this solution in place but if your interviewer argue against that, then just convert to character array and reverse it, that's more safer approach.

I hope this clarifies your doubt, if you still have any doubt and something is not clear, feel free to ask.

Have a great day ahead !!

Sep 22, 2023, 12:40:22 AM


Posted to How to Reverse a String in place in Java - Example

Google apps
Main menu