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] == 'R' && s[i + 1] == 'X') {
                    swap(s, i, i + 1);
                } else if (e[i] == 'L') {
                    int j = i;
                    while (j < s.length && s[j] == 'X') {
                        j++;
                    }
                    if (s[j] != 'L') {
                        return false;
                    }
                    swap(s, i, j);
                } 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("RL", "LR")); // 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
//        System.out.println(prob.canTransform("XLXRRXXRXX", "LXXXXXXRRR")); // true
        System.out.println(prob.canTransform("XLXRRXXRXXXL", "LXXXXXXRRRLX")); // true
//        System.out.println(prob.canTransform("XLXXXXLXLX", "LLLXXXXXXX")); // true
    }
}

@@ -12,16 +12,42 @@
 tcode;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * h
@@ -371,201 +371,8 @@
  if 
-(i + 1 == s.length) %7B%0A                    return false;%0A                %7D%0A                if (s%5Bi%5D == 'R' && s%5Bi + 1%5D == 'X') %7B%0A                    swap(s, i, i + 1);%0A                %7D else if 
 (e%5Bi
@@ -547,16 +547,33 @@
     if (
+j == s.length %7C%7C 
 s%5Bj%5D != 
@@ -695,28 +695,31 @@
    %7D
- else %7B%0A
+%0A       
     
+ %7D%0A
         
     
@@ -710,32 +710,34 @@
   %7D%0A        
+%7D%0A
         
 return false
@@ -728,39 +728,81 @@
     
-return false;%0A                %7D
+for (int i = s.length - 1; i %3E= 0; i--) %7B%0A            if (s%5Bi%5D != e%5Bi%5D) %7B
 %0A   
@@ -814,103 +814,110 @@
     
-%7D%0A
     
-    %7D%0A        return true;%0A
+if (e%5Bi%5D == 'R') %7B%0A          
     
-%7D%0A%0A
+ 
     
-private static void swap(char%5B%5D chars, int i, int j
+ int j = i;%0A                    while (j %3E= 0 && s%5Bj%5D == 'X'
 ) %7B%0A
@@ -928,56 +928,27 @@
     
+   
-char tmp = chars%5Bi%5D;%0A        chars%5Bi%5D = chars%5Bj%5D
+             j--
 ;%0A  
@@ -957,999 +957,390 @@
     
+ 
-chars%5Bj%5D = tmp;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem777 prob = new Problem777();%0A//        System.out.println(prob.canTransform(%22RXXLRXRXL%22, %22XRLXXRRLX%22)); // true%0A//        System.out.println(prob.canTransform(%22RXXLRXRXL%22, %22XRLXXRRLL%22)); // false%0A//        System.out.println(prob.canTransform(%22LR%22, %22RL%22)); // false%0A//        System.out.println(prob.canTransform(%22RL%22, %22LR%22)); // false%0A//        System.out.println(prob.canTransform(%22XXXXXLXXXX%22, %22LXXXXXXXXX%22)); // true%0A//        System.out.println(prob.canTransform(%22RXXXXL%22, %22RLXXXX%22)); // true%0A//        System.out.println(prob.canTransform(%22RXXXXL%22, %22RLXXXX%22)); // true%0A//        System.out.println(prob.canTransform(%22RXXXXL%22, %22XXXXRL%22)); // true%0A//        System.out.println(prob.canTransform(%22XLXRRXXRXX%22, %22LXXXXXXRRR%22)); // true%0A        System.out.println(prob.canTransform(%22XLXRRXXRXXXL%22, %22LXXXXXXRRRLX%22)); // true%0A//        System.out.println(prob.canTransform(%22XLXXXXLXLX%22, %22LLLXXXXXXX%22)); // true
+           %7D%0A                    if (j %3C 0 %7C%7C s%5Bj%5D != 'R') %7B%0A                        return false;%0A                    %7D%0A                    swap(s, i, j);%0A                %7D%0A            %7D%0A        %7D%0A        return Arrays.equals(s, e);%0A    %7D%0A%0A    private static void swap(char%5B%5D chars, int i, int j) %7B%0A        char tmp = chars%5Bi%5D;%0A        chars%5Bi%5D = chars%5Bj%5D;%0A        chars%5Bj%5D = tmp;
 %0A   

Solve problem 777