package cn.edu.bjut.chapter6; public class StackTester { public static boolean isMatch(String str) { char[] charArray = str.toCharArray(); MyStack stack = new MyStack(); for (char c : charArray) { if (c == '(' || c == '[' || c == '{') { stack.push(c); } else if (c == ')' || c == ']' || c == '}') { if (stack.isEmpty()) { return false; } Character cc = stack.pop(); switch (c) { case ')': if (cc != '(') { return false; } break; case ']': if (cc != '[') { return false; } break; case '}': if (cc != '{') { return false; } break; } } } return stack.isEmpty() ? true : false; } public static void main(String[] args) { System.out.println(isMatch("ab(dd[123]}")); System.out.println(isMatch("ab(dd[123])")); } }