Created
August 25, 2014 23:13
-
-
Save shixiaoyu/0fe86f6996fc2a405adf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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