summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.idea/misc.xml6
-rw-r--r--.idea/vcs.xml6
-rw-r--r--src/main/Bracket.java51
-rw-r--r--src/test/BracketTest.java77
4 files changed, 140 insertions, 0 deletions
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..83f45f8
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+ <output url="file://$PROJECT_DIR$/src/out" />
+ </component>
+</project> \ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="VcsDirectoryMappings">
+ <mapping directory="" vcs="Git" />
+ </component>
+</project> \ No newline at end of file
diff --git a/src/main/Bracket.java b/src/main/Bracket.java
new file mode 100644
index 0000000..e76eeba
--- /dev/null
+++ b/src/main/Bracket.java
@@ -0,0 +1,51 @@
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.Stack;
+
+public class Bracket {
+
+ static String checkBracket(String exp) {
+ Stack<Character> charStack = new Stack<>();
+ int unmatchedOpeningBracket = 0;
+ int count = 0;
+ for (int i = 0; i < exp.length(); i++) {
+ char c = exp.charAt(i);
+ if (!Arrays.asList('[', ']', '(', ')', '{', '}').contains(c)) {
+ count = count + 1;
+ continue;
+ }
+
+ if (c == '[' || c == '(' || c == '{') {
+ charStack.push(c);
+ count = count + 1;
+ unmatchedOpeningBracket = count;
+ } else {
+ if (charStack.empty())
+ return Integer.toString(i + 1);
+ char top = charStack.pop();
+ if ((top == '[' && c != ']') || (top == '(' && c != ')') || (top == '{' && c != '}')) {
+ return Integer.toString(i + 1);
+ } else {
+ unmatchedOpeningBracket = unmatchedOpeningBracket - 1;
+ }
+ count = count + 1;
+ }
+ }
+ if (charStack.empty())
+ return "Success";
+ else if (unmatchedOpeningBracket > 0) {
+ return Integer.toString(unmatchedOpeningBracket);
+ }
+ return Integer.toString(count);
+ }
+
+ public static void main(String[] args) throws IOException {
+ InputStreamReader input_stream = new InputStreamReader(System.in);
+ BufferedReader reader = new BufferedReader(input_stream);
+ String text = reader.readLine();
+
+ System.out.println(checkBracket(text));
+ }
+}
diff --git a/src/test/BracketTest.java b/src/test/BracketTest.java
new file mode 100644
index 0000000..d554458
--- /dev/null
+++ b/src/test/BracketTest.java
@@ -0,0 +1,77 @@
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class BracketTest {
+ @Test
+ void test() {
+ String exp = "[]";
+ assertEquals("Success", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test2() {
+ String exp = "{}[]";
+ assertEquals("Success", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test3() {
+ String exp = "[()]";
+ assertEquals("Success", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test4() {
+ String exp = "(())";
+ assertEquals("Success", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test5() {
+ String exp = "{[]}()";
+ assertEquals("Success", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test6() {
+ String exp = "{";
+ assertEquals("1", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test7() {
+ String exp = "{[}";
+ assertEquals("3", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test8() {
+ String exp = "foo(bar);";
+ assertEquals("Success", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test9() {
+ String exp = "foo(bar[i);";
+ assertEquals("10", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test10() {
+ String exp = "[](()";
+ assertEquals("3", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test11() {
+ String exp = "{[}haha";
+ assertEquals("3", Bracket.checkBracket(exp));
+ }
+
+ @Test
+ void test12() {
+ String exp = "haha{[}";
+ assertEquals("7", Bracket.checkBracket(exp));
+ }
+}