Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean canTransform(String start, String end) {
- ArrayList<Character> array = new ArrayList<Character>();
- start = start.replaceAll("\"", "").replace("\\", "").replace("\\", "");
- end = end.replaceAll("\"", "").replace("\\", "").replace("\\", "");
- String rl_start = start.replaceAll("X", "");
- String rl_end = end.replaceAll("X", "");
- if (!rl_start.equals(rl_end)) { return false; }
- if (start.length() != end.length()) { return false; }
- Stack<Character> stack = new Stack<Character>();
- for (int i = 0; i < start.length(); i++) {
- if (start.charAt(i) == 'R') { stack.push(start.charAt(i)); }
- if (end.charAt(i) == 'R') {
- if (stack.empty()) {
- return false;
- } else {
- stack.pop();
- }
- }
- }
- stack = new Stack<Character>();
- for (int i = 0; i < start.length(); i++) {
- if (end.charAt(i) == 'L') { stack.push(end.charAt(i)); }
- if (start.charAt(i) == 'L') {
- if (stack.empty()) {
- return false;
- } else {
- stack.pop();
- }
- }
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment