Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. class Solution {
  2. public boolean canTransform(String start, String end) {
  3. ArrayList<Character> array = new ArrayList<Character>();
  4.  
  5. start = start.replaceAll("\"", "").replace("\\", "").replace("\\", "");
  6. end = end.replaceAll("\"", "").replace("\\", "").replace("\\", "");
  7.  
  8. String rl_start = start.replaceAll("X", "");
  9. String rl_end = end.replaceAll("X", "");
  10.  
  11. if (!rl_start.equals(rl_end)) { return false; }
  12. if (start.length() != end.length()) { return false; }
  13.  
  14. Stack<Character> stack = new Stack<Character>();
  15. for (int i = 0; i < start.length(); i++) {
  16. if (start.charAt(i) == 'R') { stack.push(start.charAt(i)); }
  17. if (end.charAt(i) == 'R') {
  18. if (stack.empty()) {
  19. return false;
  20. } else {
  21. stack.pop();
  22. }
  23. }
  24. }
  25.  
  26. stack = new Stack<Character>();
  27. for (int i = 0; i < start.length(); i++) {
  28. if (end.charAt(i) == 'L') { stack.push(end.charAt(i)); }
  29. if (start.charAt(i) == 'L') {
  30. if (stack.empty()) {
  31. return false;
  32. } else {
  33. stack.pop();
  34. }
  35. }
  36. }
  37.  
  38. return true;
  39. }
  40. }
Add Comment
Please, Sign In to add comment