 <p>It is a very clear algorithm and seems you got it right. So for changing the way of accessing the characters, why didn't you use the <code>String#charAt</code> method?</p> <p>Also you may want to change the return type of the method from String <code>"YES"</code> or <code>"NO"</code> to <code>true</code> or <code>false</code> of type <code>boolean</code>:</p> <pre><code>static boolean isValidBracketString(String string) { Stack&lt;Character&gt; stack = new Stack&lt;&gt;(); for(int i=0; i&lt; string.length(); i++){ if(string.charAt(i) == '{' || string.charAt(i) == '[' || string.charAt(i) == '('){ stack.push(string.charAt(i)); } else if(string.charAt(i) == '}' || string.charAt(i) == '}' || string.charAt(i) == ')') { if(stack.size() == 0) return false; switch(stack.pop()){ case '(': if(string.charAt(i) != ')') return false; break; case '[': if(string.charAt(i) != ']') return false; break; case '{': if(string.charAt(i) != '}') return false; break; } } } return stack.size() == 0; } </code></pre>