What is the minimum number of push operation in reversing A B C D in stack?

I Have coded to reverse the ABCD string with using push and pop operation in Javascript

var count = 0;
function reverse(str) {
    let stack = [];
    // push letter into stack
    for (let i = 0; i < str.length; i++) {
        stack.push(str[i]);
        count++
    }
    // pop letter from the stack
    let reverseStr = '';
    while (stack.length > 0) {
        reverseStr += stack.pop();
    }
    return reverseStr;
}

console.log(reverse("ABCD") + count);

What is the minimum number of push operation in reversing ABCD?

1 answer

  • answered 2020-10-01 06:22 Abdul Hameed

    you don't even need the push operations if you are using JS str.split('').reverse().join('') and by the way it only takes 3 push operations.