-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks.java
More file actions
142 lines (124 loc) · 4.06 KB
/
Copy pathStacks.java
File metadata and controls
142 lines (124 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.util.*;
public class Stacks {
public static void main(String[] args) {
// System.out.println(isValid("{[()]}")); // Output: true
// System.out.println(isValid("{[(])}")); // Output: false
// MinStack minStack = new MinStack();
// minStack.push(-2);
// minStack.push(0);
// minStack.push(-3);
// System.out.println(minStack.getMin()); // Output: -3
// minStack.pop();
// System.out.println(minStack.top()); // Output: 0
// System.out.println(minStack.getMin()); // Output: -2
System.out.println(removeConscutive("aabbccc", 2)); // Output: "c"
System.out.println(removeConscutive("abbccaa", 2)); // Output: "a"
System.out.println(removeConscutive("aaacbbbcc", 3)); // Output:
}
// Check for balance paranthesis
public static boolean isValid(String str) {
HashMap<Character, Character> charMap = new HashMap<Character, Character>();
charMap.put(')', '(');
charMap.put('}', '{');
charMap.put(']', '[');
Stack<Character> stack = new Stack<>();
for (char c : str.toCharArray()) {
if (charMap.containsKey(c)) {
char topElement = stack.isEmpty() ? '#' : stack.pop();
if (charMap.get(c) != topElement) {
return false;
}
} else if (c == '(' || c == '{' || c == '[') {
stack.push(c);
}
}
return stack.isEmpty();
}
public static int reversePolish(String[] arr) {
Stack<Integer> stack = new Stack<>();
int num1, num2;
for (String s : arr) {
switch (s) {
case "+":
num2 = stack.pop();
num1 = stack.pop();
stack.push(num1 + num2);
break;
case "-":
num2 = stack.pop();
num1 = stack.pop();
stack.push(num1 - num2);
break;
case "*":
num2 = stack.pop();
num1 = stack.pop();
stack.push(num1 * num2);
break;
case "/":
num2 = stack.pop();
num1 = stack.pop();
stack.push(num1 / num2);
break;
default:
stack.push(Integer.parseInt(s));
break;
}
}
return stack.pop();
}
public static String removeConscutive(String s, int k) {
Stack<Pair> stack = new Stack<Pair>();
for (char c : s.toCharArray()) {
if (!stack.isEmpty() && stack.peek().character == c) {
Pair top = stack.pop();
top.count++;
if (top.count < k) {
stack.push(top);
}
} else {
stack.push(new Pair(c, 1));
}
}
// Build the result string from the stack
StringBuilder result = new StringBuilder();
while (!stack.isEmpty()) {
Pair top = stack.pop();
for (int i = 0; i < top.count; i++) {
result.append(top.character);
}
}
return result.reverse().toString();
}
}
class MinStack {
private Stack<Integer> mainStack;
private Stack<Integer> minStack;
public MinStack() {
mainStack = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
mainStack.push(val);
if (minStack.isEmpty() || val <= minStack.peek()) {
minStack.push(val);
}
}
public void pop() {
if (mainStack.pop().equals(minStack.peek()))
minStack.pop();
}
public int top() {
return mainStack.peek();
}
public int getMin() {
return minStack.peek();
}
}
class Pair {
char character;
int count;
Pair(char character, int count) {
this.character = character;
this.count = count;
}
}