forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalancingParenthesis.java
160 lines (138 loc) · 3.63 KB
/
BalancingParenthesis.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.rampatra.stacks;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.EmptyStackException;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 9/15/15
*/
public class BalancingParenthesis {
private static final char L_PAREN = '(';
private static final char R_PAREN = ')';
private static final char L_BRACE = '{';
private static final char R_BRACE = '}';
private static final char L_BRACKET = '[';
private static final char R_BRACKET = ']';
/**
* Checks if the parenthesis are well-formed in string {@param input}.
* <p/>
* For example,
* {[()]} : true
* {[]()[]} : true
* {[)} : false
* {(} : false
*
* @param input
* @return {@code true} if parenthesis are well-formed, {@code false} otherwise.
*/
public static boolean isWellFormed(String input) {
int len = input.length();
Stack<Character> stack = new Stack<>();
// obvious check as the i/p only consists of parenthesis
if (len % 2 != 0) return false;
for (int i = 0; i < len; i++) {
char charAtI = input.charAt(i);
if (charAtI == L_PAREN || charAtI == L_BRACE || charAtI == L_BRACKET) {
stack.push(charAtI);
} else if (charAtI == R_PAREN) {
if (stack.isEmpty() || stack.pop() != L_PAREN) return false;
} else if (charAtI == R_BRACE) {
if (stack.isEmpty() || stack.pop() != L_BRACE) return false;
} else if (charAtI == R_BRACKET) {
if (stack.isEmpty() || stack.pop() != L_BRACKET) return false;
}
}
return stack.isEmpty();
}
/**
* Reads the file specified in {@param filePath} line by line
* and checks if parenthesis are well-formed or not.
*
* @param filePath
*/
public static void readFile(String filePath) {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(isWellFormed(line) ? "True" : "False");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Starting point of the program.
*
* @param args
*/
public static void main(String[] args) {
readFile(args[0]);
}
}
/**
* Stack implementation using
* a singly linked list.
*
* @param <E>
*/
class Stack<E> {
private Node<E> top;
public Stack() {
top = null;
}
/**
* Pushes an item onto the top of this stack.
*
* @param item
*/
public E push(E item) {
top = new Node<>(item, top);
return item;
}
/**
* Removes the object at the top of this stack and
* returns it.
*
* @return
*/
public E pop() {
E item = peek();
top = top.next;
return item;
}
/**
* Looks at the object at the top of this stack without removing it from the stack.
*
* @return
*/
public E peek() {
if (top == null) {
throw new EmptyStackException();
}
return top.item;
}
/**
* Tests if this stack is empty.
*
* @return
*/
public boolean isEmpty() {
return top == null;
}
/**
* Generic node for holding values of any object type.
*
* @param <E>
*/
private class Node<E> {
E item;
Node<E> next;
Node(E item, Node<E> next) {
this.item = item;
this.next = next;
}
}
}