package leetcode;

/**
 * https://leetcode.com/problems/push-dominoes/
 */
public class Problem838 {
    public String pushDominoes(String dominoes) {
        char[] chars = new char[dominoes.length()];
        int cur = 0;
        int prev = 0;
        while (cur < dominoes.length()) {
            chars[cur] = dominoes.charAt(cur);
            if (dominoes.charAt(cur) == 'L') {
                if (dominoes.charAt(prev) == 'R') {
                    int mid = (prev + cur) / 2;
                    int mod = (prev + cur) % 2;
                    if (mod > 0) {
                        for (int i = prev; i <= mid; i++) {
                            chars[i] = 'R';
                        }
                        for (int i = mid + 1; i <= cur; i++) {
                            chars[i] = 'L';
                        }
                    } else {
                        for (int i = prev; i < mid; i++) {
                            chars[i] = 'R';
                        }
                        for (int i = mid + 1; i <= cur; i++) {
                            chars[i] = 'L';
                        }
                    }
                } else {
                    for (int i = prev; i <= cur; i++) {
                        chars[i] = 'L';
                    }
                }
                prev = cur;
            } else if (dominoes.charAt(cur) == 'R') {
                prev = cur;
                // TODO
            }
            cur++;
        }
        if (dominoes.charAt(prev) == 'R') {
            for (int i = prev; i < dominoes.length(); i++) {
                chars[i] = 'R';
            }
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        Problem838 prob = new Problem838();
//        System.out.println(prob.pushDominoes(".L.R...LR..L..")); // LL.RR.LLRRLL..
//        System.out.println(prob.pushDominoes("R..L")); // RRLL
//        System.out.println(prob.pushDominoes("R...L")); // RR.LL
//        System.out.println(prob.pushDominoes("RR.L")); // RR.L
//        System.out.println(prob.pushDominoes("R..L")); // RRLL
//        System.out.println(prob.pushDominoes("R.LL")); // R.LL
//        System.out.println(prob.pushDominoes("...")); // ...
//        System.out.println(prob.pushDominoes("L..")); // L..
//        System.out.println(prob.pushDominoes("..L")); // LLL
//        System.out.println(prob.pushDominoes("..R")); // ..R
//        System.out.println(prob.pushDominoes("R..")); // RRR
//        System.out.println(prob.pushDominoes("..R..")); // ..RRR
//        System.out.println(prob.pushDominoes("..L..")); // LLL..
//        System.out.println(prob.pushDominoes("LLL")); // LLL
//        System.out.println(prob.pushDominoes("RRR")); // RRR
//        System.out.println(prob.pushDominoes("LR")); // LR
//        System.out.println(prob.pushDominoes("RL")); // LR
//        System.out.println(prob.pushDominoes("R.R.L")); // RRR.L
        System.out.println(prob.pushDominoes("R.R")); // RRR
    }
}

@@ -1397,43 +1397,207 @@
     
+if (dominoes.charAt(
 prev
+)
  =
- cur;%0A                // TODO
+= 'R') %7B%0A                    for (int i = prev; i %3C= cur; i++) %7B%0A                        chars%5Bi%5D = 'R';%0A                    %7D%0A                %7D%0A                prev = cur;
 %0A   
@@ -1789,32 +1789,32 @@
     %7D%0A        %7D%0A
+
         return n
@@ -1840,1340 +1840,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem838 prob = new Problem838();%0A//        System.out.println(prob.pushDominoes(%22.L.R...LR..L..%22)); // LL.RR.LLRRLL..%0A//        System.out.println(prob.pushDominoes(%22R..L%22)); // RRLL%0A//        System.out.println(prob.pushDominoes(%22R...L%22)); // RR.LL%0A//        System.out.println(prob.pushDominoes(%22RR.L%22)); // RR.L%0A//        System.out.println(prob.pushDominoes(%22R..L%22)); // RRLL%0A//        System.out.println(prob.pushDominoes(%22R.LL%22)); // R.LL%0A//        System.out.println(prob.pushDominoes(%22...%22)); // ...%0A//        System.out.println(prob.pushDominoes(%22L..%22)); // L..%0A//        System.out.println(prob.pushDominoes(%22..L%22)); // LLL%0A//        System.out.println(prob.pushDominoes(%22..R%22)); // ..R%0A//        System.out.println(prob.pushDominoes(%22R..%22)); // RRR%0A//        System.out.println(prob.pushDominoes(%22..R..%22)); // ..RRR%0A//        System.out.println(prob.pushDominoes(%22..L..%22)); // LLL..%0A//        System.out.println(prob.pushDominoes(%22LLL%22)); // LLL%0A//        System.out.println(prob.pushDominoes(%22RRR%22)); // RRR%0A//        System.out.println(prob.pushDominoes(%22LR%22)); // LR%0A//        System.out.println(prob.pushDominoes(%22RL%22)); // LR%0A//        System.out.println(prob.pushDominoes(%22R.R.L%22)); // RRR.L%0A        System.out.println(prob.pushDominoes(%22R.R%22)); // RRR%0A    %7D
 %0A%7D%0A

Solve problem 838