We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Let take this example: {}}}{}
1.Value inserted into Old stack : bottom[ {,},},},{,} ]top
2.Now it will pop first element i.e. “}”
3.Since it satisfy this condition : if(popElement.contains("}") || popElement.contains(")") || popElement.contains("]”))
This value “}” will be pushed in newStack New Stack : bottom[ } ]top
4.Now it will pop second element i.e. “{“ Since it satisfy this condition /*else{ String popElementPair = hMap.get(popElement); String popElementNewStack = newStack.pop(); if(popElementPair.equals(popElementNewStack)){ casePass=true; }else{ casePass=false; } } */
5.Now this key:”{“ has value “}”, so i will pop value “}" from new stack. Old stack : bottom[ {,},},} ]top ; New Stack : bottom[ ]top
6.This goes on in loop
7.In the end situation will be
Old stack : bottom[ ]top
New Stack : bottom[ },} ]top
And case will get failed since new stack is not empty.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Stack
You are viewing a single comment's thread. Return to all comments →
Let take this example: {}}}{} 1.Value inserted into Old stack : bottom[ {,},},},{,} ]top 2.Now it will pop first element i.e. “}” 3.Since it satisfy this condition : if(popElement.contains("}") || popElement.contains(")") || popElement.contains("]”)) This value “}” will be pushed in newStack New Stack : bottom[ } ]top 4.Now it will pop second element i.e. “{“ Since it satisfy this condition /*else{ String popElementPair = hMap.get(popElement); String popElementNewStack = newStack.pop(); if(popElementPair.equals(popElementNewStack)){ casePass=true; }else{ casePass=false; } } */ 5.Now this key:”{“ has value “}”, so i will pop value “}" from new stack. Old stack : bottom[ {,},},} ]top ; New Stack : bottom[ ]top 6.This goes on in loop 7.In the end situation will be Old stack : bottom[ ]top New Stack : bottom[ },} ]top And case will get failed since new stack is not empty.