package leetcode;

/**
 * https://leetcode.com/problems/swap-adjacent-in-lr-string/
 */
public class Problem777 {
    public boolean canTransform(String start, String end) {
        char[] s = start.toCharArray();
        char[] e = end.toCharArray();
        for (int i = 0; i < s.length; i++) {
            if (s[i] != e[i]) {
                if (i + 1 == s.length) {
                    return false;
                }
                if ((s[i] == 'X' && s[i + 1] == 'L') ||
                    (s[i] == 'R' && s[i + 1] == 'X')) {
                    swap(e, i, i + 1);
                    if (s[i] != e[i]) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    private static void swap(char[] chars, int i, int j) {
        char tmp = chars[i];
        chars[i] = chars[j];
        chars[j] = tmp;
    }

    public static void main(String[] args) {
        Problem777 prob = new Problem777();
//        System.out.println(prob.canTransform("RXXLRXRXL", "XRLXXRRLX")); // true
//        System.out.println(prob.canTransform("RXXLRXRXL", "XRLXXRRLL")); // false
//        System.out.println(prob.canTransform("LR", "RL")); // false
        System.out.println(prob.canTransform("XXXXXLXXXX", "LXXXXXXXXX")); // true
        System.out.println(prob.canTransform("RXXXXL", "RLXXXX")); // true
        System.out.println(prob.canTransform("RXXXXL", "RLXXXX")); // true
        System.out.println(prob.canTransform("RXXXXL", "XXXXRL")); // true
    }
}

@@ -434,17 +434,16 @@
      if 
-(
 (s%5Bi%5D ==
@@ -444,17 +444,17 @@
 %5Bi%5D == '
-X
+R
 ' && s%5Bi
@@ -467,14 +467,13 @@
 == '
-L') %7C%7C
+X') %7B
 %0A   
@@ -489,39 +489,154 @@
         
-(s%5Bi%5D == 'R'
+swap(s, i, i + 1);%0A                %7D else if (e%5Bi%5D == 'L') %7B%0A                    int j = i;%0A                    while (j %3C s.length
  && s%5B
-i + 1
+j
 %5D == 'X'
@@ -636,17 +636,16 @@
  == 'X')
-)
  %7B%0A     
@@ -663,26 +663,38 @@
     
-swap(e, i, i + 1);
+    j++;%0A                    %7D
 %0A   
@@ -712,34 +712,33 @@
       if (s%5B
-i
+j
 %5D != 
-e%5Bi%5D
+'L'
 ) %7B%0A        
@@ -781,32 +781,67 @@
               %7D%0A
+                    swap(s, i, j);%0A
                 
@@ -1427,16 +1427,88 @@
 / false%0A
+//        System.out.println(prob.canTransform(%22RL%22, %22LR%22)); // false%0A//
         
@@ -1574,32 +1574,34 @@
 XXX%22)); // true%0A
+//
         System.o
@@ -1651,32 +1651,34 @@
 XXX%22)); // true%0A
+//
         System.o
@@ -1732,24 +1732,26 @@
 )); // true%0A
+//
         Syst
@@ -1813,16 +1813,99 @@
 // true%0A
+        System.out.println(prob.canTransform(%22XLXRRXXRXX%22, %22LXXXXXXRRR%22)); // true%0A
     %7D%0A%7D%0A

Update problem 777