Skip to content

Instantly share code, notes, and snippets.

@shixiaoyu
Created August 25, 2014 23:13
Show Gist options
  • Save shixiaoyu/0fe86f6996fc2a405adf to your computer and use it in GitHub Desktop.
Save shixiaoyu/0fe86f6996fc2a405adf to your computer and use it in GitHub Desktop.
private String y = "Possible";
private String n = "Impossible";
public String ableToSolve(String s) {
if (s == null) return n;
if (s.isEmpty()) return y;
if (s.length() % 2 != 0) return n;
Stack<Character> stack = new Stack<Character>();
for(int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (!stack.isEmpty() && stack.peek().equals(c)) {
stack.pop();
} else {
stack.push(c);
}
}
return stack.isEmpty() ? y : n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment