Alphabet Board Path | String Problem | LeetCode 1138

4 years ago Lalit Bhagtani 0

Problem Statement

You have given an alphabet board, board = [“abcde”, “fghij”, “klmno”, “pqrst”, “uvwxy”, “z”], as shown in the diagram below.

Alphabet Board

On an alphabet board, you start at a position (0, 0), corresponding to character board[0][0] and you can take only the following moves:

  1. ‘U’ moves your position up one row, if the position exists on the board.
  2. ‘D’ moves your position down one row, if the position exists on the board.
  3. ‘L’ moves your position left one column, if the position exists on the board.
  4. ‘R’ moves your position right one column, if the position exists on the board.
  5. ‘!’ adds the character board[row][column] 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 you answer equal to the target in the minimum number of moves. You may return any path that does so.

Example 

Alphabet Board Path

Input :- "hello"
Output :- DRR!URR!DDLLL!!RRR!
Input :- "morning"
Output :- DDRR!RR!DLL!UR!U!D!ULL!

Program

public class Main {

	public static void main(String[] args) {		
		Main main = new Main();
		String result = main.alphabetBoardPath("hello");
		System.out.println(result);
	}
	
	/* Solution */
	public String alphabetBoardPath(String target) {
        
        char[] array = target.toCharArray();
        StringBuilder sb = new StringBuilder();
        char[][] board = new char[][] {{'a','b','c','d','e'}, {'f','g','h','i','j'},
                          {'k','l','m','n','o'}, {'p','q','r','s','t'},
                          {'u','v','w', 'x','y'}, {'z'} };
        int prerow = 0, precolumn = 0, row = 0, column = 0;
        for(char c : array){            
            row = getRow(c);
            column = getColumn(c, row);
            sb.append(findPath(c, board, prerow, precolumn, row, column));
            prerow = row;
            precolumn = column;
        }
        return sb.toString();
    }
    
    public int getRow(char c){        
        if(c >= 'a' && c <='e'){
            return 0;
        }else if(c >= 'f' && c <='j'){
            return 1;
        }else if(c >= 'k' && c <='o'){
            return 2;
        }else if(c >= 'p' && c <='t'){
            return 3;
        }else if(c >= 'u' && c <='y'){
            return 4;
        }else {
            return 5;
        }        
    }
    
    public int getColumn(char c, int row){        
        if(row == 0){
            return c - 'a';
        }else if(row == 1){
            return c - 'f';
        }else if(row == 2){
            return c - 'k';
        }else if(row == 3){
            return c - 'p';
        }else if(row == 4){
            return c - 'u';
        }else {
            return 0;
        }     
    }
    
    public String findPath(char c, char[][] board, int srow, int scolumn, int drow, int dcolumn){
       
        String result = "";        
        boolean isZ = false;
        
        if(drow != srow){
            if(drow == 5){
                isZ = true;
                drow--;
            }

            if(drow > srow){        
                while(drow > srow){
                    result = result + "D";
                    srow++;
                }
            }else {
                while(drow < srow){
                    result = result + "U";
                    drow++;
                }
            }
        }        
        
        if(dcolumn > scolumn){
            while(dcolumn > scolumn){
                result = result + "R";
                scolumn++;
            }
        }else {
            while(dcolumn < scolumn){
                result = result + "L";
                dcolumn++;
            }
        }
        
        if(isZ){
            result = result + "D";
        }
        
        result = result + "!";
        return result;
    }
}

Result

DRR!URR!DDLLL!!RRR!

Similar Post

  1. Single Row Keyboard
  2. Group Anagrams
  3. Find All Anagrams in a String

That’s all for Alphabet Board Path in Java, If you liked it, please share your thoughts in a comments section and share it with others too.