Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Test cases:
- {[]
- {{()}}
- ]][[
- [[]]
- [()()]
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.charset.StandardCharsets;
- import java.util.Map;
- public class Main {
- /**
- * Iterate through each line of input.
- */
- public static void main(String[] args) throws IOException {
- InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
- BufferedReader in = new BufferedReader(reader);
- String line;
- while ((line = in.readLine()) != null) {
- char[] chars = line.toCharArray();
- // Odd number of chars will never be well-formed
- if (chars.length%2 != 0) {
- System.out.println("False");
- continue;
- }
- boolean isWellFormed = true;
- for (int i = 0; i < chars.length/2; i++) {
- char lhs = chars[i];
- char rhs = chars[chars.length-i-1];
- if (!charsMatch(lhs, rhs)) {
- isWellFormed = false;
- break;
- }
- }
- if (isWellFormed) {
- System.out.println("True");
- } else {
- System.out.println("False");
- }
- }
- }
- private static boolean charsMatch(char lhs, char rhs) {
- return (lhs == '(' && rhs == ')') ||
- (lhs == '[' && rhs == ']') ||
- (lhs == '{' && rhs == '}');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement