- Push the character one by one into the Stack of datatype character.
- Pop the character one by one from the Stack until the stack becomes empty.
- Add a popped element to the character array.
- Convert character array to string.
- Return reversed string.
Below is the implementation of the above approach.
GeeksForGeeks <- Reverse -> skeeGroFskeeG Hello World <- Reverse -> dlroW olleH
Time Complexity: O(n), where n is a number of characters in the stack.
Auxiliary Space: O(n) for the stack.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.