Problem3_1 { public int firstOffendingParenthesis(String input) { Stack<Integer> stack = new Stack<>(); for (int i = 0; i < input.length(); i++) { if (input.charAt(i) == ')' && !stack.empty()) { stack.pop(); } else { stack.push(i); } } return stack.empty() ? -1 : stack.elementAt(0); }  boolean isBalancedString(final String input); int firstOffendingParenthesis(String input);  }
@Test public void shouldFindPositionOfFirstOffendingParenthesis() throws Exception { final String input = ")()("; int position = checker.firstOffendingParenthesis(input); assertThat(position, equalTo(0)); }
@Test public void shouldFindPositionOfFirstOffendingParenthesis_input2() throws Exception { final String input = "(())("; int position = checker.firstOffendingParenthesis(input); assertThat(position, equalTo(4)); }
@Test public void shouldFindPositionOfFirstOffendingParenthesis_input3() throws Exception { final String input = "())"; int position = checker.firstOffendingParenthesis(input); assertThat(position, equalTo(2)); }