(Medium) Alphabet Board Path - LeetCode

1138. Alphabet Board Path

  On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].

Here, board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"], as shown in the diagram below.

We may make the following moves:

  • 'U' moves our position up one row, if the position exists on the board;
  • 'D' moves our position down one row, if the position exists on the board;
  • 'L' moves our position left one column, if the position exists on the board;
  • 'R' moves our position right one column, if the position exists on the board;
  • '!' adds the character board[r][c] at our current position (r, c) to the answer.

(Here, the only positions that exist on the board are positions with letters on them.)

Return a sequence of moves that makes our answer equal to target in the minimum number of moves.  You may return any path that does so.

 

Example 1:

Input: target = "leet"
Output: "DDR!UURRR!!DDD!"

Example 2:

Input: target = "code"
Output: "RR!DDRR!UUL!R!"

 

Constraints:

  • 1 <= target.length <= 100
  • target consists only of English lowercase letters.

 

class Solution {
    public String alphabetBoardPath(String target) {
       
        String[] board= {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
       
        if(target ==null || target.length() ==0){
            return null;
        }
       
        String out="";
       
        int prev_x =0;
        int prev_y =0;
       
        int row = 0;
        int col = 0;
       
       
       
        for(int i =0; i<target.length();i++){
           
            char key = target.charAt(i);
           
            if(i>0 && target.charAt(i)==target.charAt(i-1)){
                out = out+"!";
                continue;
            }
           
            for(int j =0; j< board.length;j++){
               
                if(board[j].indexOf(key)!= -1){
                   
                    row = j-row;
                    col = board[j].indexOf(key)-col;
                   
                    if(row >0){
                       
                        if(key =='z' && prev_y!=0){
                           
                            out = out+repeat("D",row -1);
                        }
                       
                        else{
                              out = out + repeat("D",row);
                        }
                     
                    }
                   
                    else{
                        out =out +repeat("U",(-row));
                       
                    }
                   
                    if(col>0){
                       
                        out = out + repeat("R",col);
                    }
                   
                    else {
                       
                        if(key =='z' && prev_y!=0){
                           
                             out = out +repeat("L", -col);
                           
                            out = out+"D";
                        }
                       
                        else{
                             out = out +repeat("L", -col);
                           
                        }
                       
                       
                    }
                   
                    out=out+"!";
                   
                    row =j;
                    col = board[j].indexOf(key);
                   
                    prev_x = j;
                    prev_y = col;
                    break;
                }
            }
           
          
        }
       
        return out;
       
    }
   
    public  String repeat(String a, int b){
   
    String out ="";
   
    for(int i =0;i<b;i++)
    {
      out = out+a;
    }
     return out;
  }
   
   
}

 

posted @ 2019-08-02 15:57  CodingYM  阅读(318)  评论(0编辑  收藏  举报