|
|
@ -1,4 +1,6 @@
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
import java.math.*;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Template code to help you parse the standard input
|
|
|
|
* Template code to help you parse the standard input
|
|
|
@ -9,34 +11,10 @@ class Solution {
|
|
|
|
public static void main( String[] args ) {
|
|
|
|
public static void main( String[] args ) {
|
|
|
|
Scanner in = new Scanner( System.in );
|
|
|
|
Scanner in = new Scanner( System.in );
|
|
|
|
// read values with in.next...() methods
|
|
|
|
// read values with in.next...() methods
|
|
|
|
String input = in.nextLine();
|
|
|
|
|
|
|
|
// code your solution here
|
|
|
|
|
|
|
|
boolean output = isInputWellFormed(input);
|
|
|
|
|
|
|
|
// Write result with System.out.println()
|
|
|
|
|
|
|
|
System.out.println( output );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static boolean isInputWellFormed(String input) {
|
|
|
|
|
|
|
|
HashMap<Character, Character> validPairs = new HashMap<>();
|
|
|
|
|
|
|
|
validPairs.put('{', '}');
|
|
|
|
|
|
|
|
validPairs.put('(', ')');
|
|
|
|
|
|
|
|
validPairs.put('[', ']');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Deque<Character> unmatchedChars = new ArrayDeque<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < input.length(); i++) {
|
|
|
|
// code your solution here
|
|
|
|
char currentChar = input.charAt(i);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (validPairs.keySet().contains(currentChar)) {
|
|
|
|
// Write result with System.out.println()
|
|
|
|
unmatchedChars.push(currentChar); // Add only opening ({[ to the unmatchedChars.
|
|
|
|
System.out.println( "value" );
|
|
|
|
} else if (validPairs.values().contains(currentChar)) {
|
|
|
|
|
|
|
|
if (!unmatchedChars.isEmpty() && validPairs.get(unmatchedChars.peekFirst()) == currentChar) {
|
|
|
|
|
|
|
|
unmatchedChars.pop(); // Match found, empty the unmatchedChars.
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return unmatchedChars.isEmpty();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|