SHARE
TWEET

Untitled

a guest Oct 14th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Test cases:
  2. {[]
  3. {{()}}
  4. ]][[
  5. [[]]
  6. [()()]
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.nio.charset.StandardCharsets;
  12. import java.util.Map;
  13.  
  14. public class Main {
  15.     /**
  16.      * Iterate through each line of input.
  17.      */
  18.     public static void main(String[] args) throws IOException {
  19.         InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
  20.         BufferedReader in = new BufferedReader(reader);
  21.         String line;
  22.         while ((line = in.readLine()) != null) {
  23.             char[] chars = line.toCharArray();
  24.             // Odd number of chars will never be well-formed
  25.             if (chars.length%2 != 0) {
  26.                 System.out.println("False");
  27.                 continue;
  28.             }
  29.  
  30.             boolean isWellFormed = true;
  31.             for (int i = 0; i < chars.length/2; i++) {
  32.                 char lhs = chars[i];
  33.                 char rhs = chars[chars.length-i-1];
  34.                
  35.                 if (!charsMatch(lhs, rhs)) {
  36.                     isWellFormed = false;
  37.                     break;
  38.                 }
  39.             }
  40.  
  41.             if (isWellFormed) {
  42.                 System.out.println("True");
  43.             } else {
  44.                 System.out.println("False");
  45.             }
  46.         }
  47.     }
  48.  
  49.     private static boolean charsMatch(char lhs, char rhs) {
  50.         return (lhs == '(' && rhs == ')') ||
  51.                 (lhs == '[' && rhs == ']') ||
  52.                 (lhs == '{' && rhs == '}');
  53.     }
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top