From 048f165373e5cb67e0ec57cb234c40fa52c1d78a Mon Sep 17 00:00:00 2001 From: Rohweini Date: Thu, 30 Apr 2020 08:10:02 +0200 Subject: [PATCH] my solution --- src/main/java/Solution.java | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/Solution.java b/src/main/java/Solution.java index 5615be4..a3880cc 100644 --- a/src/main/java/Solution.java +++ b/src/main/java/Solution.java @@ -1,6 +1,4 @@ import java.util.*; -import java.io.*; -import java.math.*; /** * Template code to help you parse the standard input @@ -11,10 +9,34 @@ class Solution { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); // 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( "value" ); + System.out.println( output ); + } + + private static boolean isInputWellFormed(String input) { + HashMap validPairs = new HashMap<>(); + validPairs.put('{', '}'); + validPairs.put('(', ')'); + validPairs.put('[', ']'); + + Deque unmatchedChars = new ArrayDeque<>(); + + for (int i = 0; i < input.length(); i++) { + char currentChar = input.charAt(i); + + if (validPairs.keySet().contains(currentChar)) { + unmatchedChars.push(currentChar); // Add only opening ({[ to the unmatchedChars. + } 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(); } }