title: Codility : Brackets Determine whether a given string of parentheses is properly nested
body: <p><strong>Problem description from codility :</strong></p>
  
  <p>A string S consisting of N characters is considered to be properly nested if any of the following conditions is true:</p>
  
  <p>S is empty;
  S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
  S has the form "VW" where V and W are properly nested strings.
  For example, the string "{[()()]}" is properly nested but "([)()]" is not.</p>
  
  <p>Write a function:</p>
  
  <p>class Solution { public int solution(String S); }</p>
  
  <p>that, given a string S consisting of N characters, returns 1 if S is properly nested and 0 otherwise.</p>
  
  <p>For example, given S = "{[()()]}", the function should return 1 and given S = "([)()]", the function should return 0, as explained above.</p>
  
  <p>Assume that:</p>
  
  <p>N is an integer within the range [0..200,000];
  string S consists only of the following characters: "(", "{", "[", "]", "}" and/or ")".
  Complexity:</p>
  
  <p>expected worst-case time complexity is O(N);
  expected worst-case space complexity is O(N) (not counting the storage required for input arguments).</p>
  
  <p><strong>I get 87% I cant seem to figure out the problem.</strong></p>
  
  <p><img src="https://i.stack.imgur.com/tKzkf.png" alt="enter image description here"></p>
  
  <p><strong>Here is my code :</strong> </p>
  
  <pre><code>   // you can also use imports, for example:
  // import java.util.*;
  import java.util.Stack;
  // you can use System.out.println for debugging purposes, e.g.
  // System.out.println("this is a debug message");
  
  class Solution {
     public int solution(String s) {
  
          if (s.length() % 2 != 0) {
              return 0;
          }
  
          Character openingBrace = new Character('{');
          Character openingBracket = new Character('[');
          Character openingParen = new Character('(');
          Stack&lt;Character&gt; openingStack = new Stack&lt;Character&gt;();
  
          for (int i = 0; i &lt; s.length(); i++) {
              char c = s.charAt(i);
              if (c == openingBrace || c == openingBracket || c == openingParen) {
                  openingStack.push(c);
              } else  {
                  if (i == s.length()-1 &amp;&amp; openingStack.size() != 1) {
                      return 0;
                  }
                  if (openingStack.isEmpty()) {
                      return 0;
                  }
                  Character openingCharacter = openingStack.pop();
                  switch (c) {
                  case '}':
                      if (!openingCharacter.equals(openingBrace)) {
                          return 0;
                      }
                      break;
                  case ']':
                      if (!openingCharacter.equals(openingBracket)) {
                          return 0;
                      }
                      break;
                  case ')':
                      if (!openingCharacter.equals(openingParen)) {
                          return 0;
                      }
                      break;
  
                  default:
                      break;
                  }
              } 
          }
  
          return 1;
  
      }
  }
  </code></pre>