package leetcode;

/**
 * https://leetcode.com/problems/alphabet-board-path/
 */
public class Problem1138 {
    public String alphabetBoardPath(String target) {
        String answer = "";
        target = "a" + target; // start from (0, 0)
        for (int i = 0; i < target.length() - 1; i++) {
            char src = target.charAt(i);
            char dest = target.charAt(i + 1);
            int srcRow = (src - 'a') / 5;
            int srcCol = (src - 'a') % 5;
            int destRow = (dest - 'a') / 5;
            int destCol = (dest - 'a') % 5;

            int row = destRow - srcRow;
            int col = destCol - srcCol;
            answer += buildPath(row, col);
        }
        return answer;
    }

    private static String buildPath(int row, int col) {
        String path  = "";
        if (row < 0) {
            for (int i = row; i < 0; i++) {
                path += "U";
            }
        } else if (row > 0) {
            for (int i = 0; i < row; i++) {
                path += "D";
            }
        }

        if (col < 0) {
            for (int i = col; i < 0; i++) {
                path += "L";
            }
        } else if (col > 0) {
            for (int i = 0; i < col; i++) {
                path += "R";
            }
        }
        return path + "!";
    }

    public static void main(String[] args) {
        Problem1138 prob = new Problem1138();
//        System.out.println(prob.alphabetBoardPath("leet")); // "DDR!UURRR!!DDD!"
//        System.out.println(prob.alphabetBoardPath("code")); // "RR!DDRR!UUL!R!"
        System.out.println(prob.alphabetBoardPath("zdz")); // "DDDDD!UUUUURRR!DDDDLLLD!"
    }
}
@@ -377,16 +377,103 @@
 i + 1);%0A
+            if (src == dest) %7B%0A                answer += %22!%22;%0A            %7D else %7B%0A    
         
@@ -498,32 +498,36 @@
 src - 'a') / 5;%0A
+    
             int 
@@ -556,32 +556,36 @@
  5;%0A            
+  
+  
 int destRow = (d
@@ -592,32 +592,36 @@
 est - 'a') / 5;%0A
+    
             int 
@@ -649,16 +649,20 @@
 ) %25 5;%0A%0A
+    
         
@@ -701,24 +701,28 @@
             
+    
 int col = de
@@ -737,16 +737,20 @@
 srcCol;%0A
+    
         
@@ -781,18 +781,57 @@
 row, col
-);
+, destRow == 5 /* is z */);%0A            %7D
 %0A       
@@ -915,16 +915,29 @@
  int col
+, boolean isZ
 ) %7B%0A    
@@ -1131,19 +1131,37 @@
  0; i %3C 
-row
+(isZ ? row - 1 : row)
 ; i++) %7B
@@ -1464,378 +1464,80 @@
     
-return path + %22!%22;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1138 prob = new Problem1138();%0A//        System.out.println(prob.alphabetBoardPath(%22leet%22)); // %22DDR!UURRR!!DDD!%22%0A//        System.out.println(prob.alphabetBoardPath(%22code%22)); // %22RR!DDRR!UUL!R!%22%0A        System.out.println(prob.alphabetBoardPath(%22zdz%22)); // %22DDDDD!UUUUURRR!DDDDLLLD
+if (isZ) %7B%0A            path += %22D%22;%0A        %7D%0A        return path + %22
 !%22
+;
 %0A   

Solve probelm 1138