Reverse String

Given a string S, reverse the string using stack.

Example :

Input : "abc"
Return "cba"
public class Solution {
    public String reverseString(String A) {
        Stack<Character> st = new Stack<>();
        for (int i = 0; i < A.length(); i++) {
            st.push(A.charAt(i));
        }
        StringBuilder str = new StringBuilder();
        while (st.size() != 0) {
            str.append(st.pop());
        }
        return str.toString();
    }
}

Last updated