Skip to content

Commit b1cb925

Browse files
committed
Introduction to Algorithms Chapter 3 3.1 problem
1 parent 14a1879 commit b1cb925

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.shekhargulati.ninetynine_problems.java8._00_random.stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested.
7+
* For example, the string ((())())() contains properly nested pairs of parentheses, which the strings )()( and ()) do not.
8+
* Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise.
9+
* For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced.
10+
*/
11+
public class ParenthesisBalanceChecker {
12+
13+
public boolean isBalancedString(final String input) {
14+
/*
15+
Algorithm:
16+
1. Iterate over all the characters in a String
17+
2. if stack is not empty and character is ')' then pop the element from the stack
18+
3. else push the element into the stack
19+
4. After iteration if stack is empty then return true else return false.
20+
*/
21+
Stack<Character> stack = new Stack<>();
22+
char[] chars = input.toCharArray();
23+
for (char ch : chars) {
24+
if (ch == ')' && !stack.empty()) {
25+
stack.pop();
26+
} else {
27+
stack.push(ch);
28+
}
29+
}
30+
return stack.isEmpty();
31+
}
32+
33+
public int firstOffendingParenthesis(String input) {
34+
/*
35+
Algorithm:
36+
1. Iterate over all the characters of input String
37+
2. If character is ')' and stack is not empty then remove element from stack
38+
3. Else add the position into the stack
39+
4. After iteration, if stack is empty then return -1 else get the first element and return its value.
40+
*/
41+
Stack<Integer> stack = new Stack<>();
42+
for (int i = 0; i < input.length(); i++) {
43+
if (input.charAt(i) == ')' && !stack.empty()) {
44+
stack.pop();
45+
} else {
46+
stack.push(i);
47+
}
48+
}
49+
return stack.empty() ? -1 : stack.elementAt(0);
50+
}
51+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.shekhargulati.ninetynine_problems.java8._00_random.stack;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.equalTo;
6+
import static org.junit.Assert.*;
7+
8+
public class ParenthesisBalanceCheckerTest {
9+
10+
private final ParenthesisBalanceChecker checker = new ParenthesisBalanceChecker();
11+
12+
@Test
13+
public void shouldSayTrueForEmptyString() throws Exception {
14+
final String input = "";
15+
boolean balanced = checker.isBalancedString(input);
16+
assertTrue(balanced);
17+
}
18+
19+
@Test
20+
public void shouldSayTrueWhenStringIsBalanced() throws Exception {
21+
final String input = "((())())()";
22+
boolean balanced = checker.isBalancedString(input);
23+
assertTrue(balanced);
24+
}
25+
26+
@Test
27+
public void shouldSayFalseWhenStringIsNotBalanced() throws Exception {
28+
final String input = ")()(";
29+
boolean balanced = checker.isBalancedString(input);
30+
assertFalse(balanced);
31+
}
32+
33+
@Test
34+
public void shouldSayFalseWhenStringIsNotBalanced_anotherInput() throws Exception {
35+
final String input = "())";
36+
boolean balanced = checker.isBalancedString(input);
37+
assertFalse(balanced);
38+
}
39+
40+
@Test
41+
public void shouldFindPositionOfFirstOffendingParenthesis() throws Exception {
42+
final String input = ")()(";
43+
int position = checker.firstOffendingParenthesis(input);
44+
assertThat(position, equalTo(0));
45+
}
46+
47+
@Test
48+
public void shouldFindPositionOfFirstOffendingParenthesis_input2() throws Exception {
49+
final String input = "(())(";
50+
int position = checker.firstOffendingParenthesis(input);
51+
assertThat(position, equalTo(4));
52+
}
53+
54+
@Test
55+
public void shouldFindPositionOfFirstOffendingParenthesis_input3() throws Exception {
56+
final String input = "())";
57+
int position = checker.firstOffendingParenthesis(input);
58+
assertThat(position, equalTo(2));
59+
}
60+
61+
}

0 commit comments

Comments
 (0)