0% found this document useful (0 votes)
17 views

Stack

The document contains code for 3 Java class methods. The first method removes asterisk characters from a string using a stack. The second method simulates asteroid collisions and returns the surviving asteroids using a stack. The third method decodes an encoded string with numeric repetition values using stacks.

Uploaded by

IG Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Stack

The document contains code for 3 Java class methods. The first method removes asterisk characters from a string using a stack. The second method simulates asteroid collisions and returns the surviving asteroids using a stack. The third method decodes an encoded string with numeric repetition values using stacks.

Uploaded by

IG Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

class Solution {

public String removeStars(String s) {


Stack<Character> st = new Stack<Character>();
for(int i = 0; i < s.length(); i++){
if(s.charAt(i)=='*') st.pop();
else st.add(s.charAt(i));
}
String res = "";
while(!st.isEmpty()){
res += st.pop();
}
String revRes = "";
for(int i = res.length()-1; i >= 0; i--){
revRes += res.charAt(i);
}
return revRes;
}
}
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();

for (int asteroid : asteroids) {


if (stack.isEmpty() || asteroid > 0) {
stack.push(asteroid);
} else {
while(true){
int peek = stack.peek();
if(peek<0){//left ones are going left already(left and right
wont explode coz moving opposite direction already....right-left is taken care of
in next conditions)
stack.push(asteroid);
break;
}
else if(peek == -asteroid){ //if right and left equal, both
explode
stack.pop();
break;
}
else if(peek>-asteroid) break;//left one going right direction
is bigger than right one going left, so the later will explode
else{//if negative is greater than the positives(right going
left is bigger than left going right)
stack.pop();
if(stack.isEmpty()){
stack.push(asteroid);
break;
}
}
}
}
}

int[] result = new int[stack.size()];


for (int i = stack.size() - 1; i >= 0; i--) {
result[i] = stack.pop();
}

return result;
}
}

class Solution {
public String decodeString(String s) {
Stack<Integer> numStack = new Stack<>();
Stack<StringBuilder> strStack = new Stack<>();
StringBuilder currentString = new StringBuilder();
int currentNum = 0;

for (char ch : s.toCharArray()) {


if (Character.isDigit(ch)) {
currentNum = currentNum * 10 + (ch - '0');
} else if (ch == '[') {
numStack.push(currentNum);
strStack.push(currentString);
currentNum = 0;
currentString = new StringBuilder();
} else if (ch == ']') {
int repeatTimes = numStack.pop();
StringBuilder decodedSubstring = strStack.pop();
for (int i = 0; i < repeatTimes; i++) {
decodedSubstring.append(currentString);
}
currentString = decodedSubstring;
} else {
currentString.append(ch);
}
}

return currentString.toString();
}
}

You might also like