TEST Flashcards

1
Q
A

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println(“this is a debug message”);

class Solution {

public String solution(int[] pin) {
    // write your code in Java 11

    String result = getKeyPressSequence(pin);
    return result;

}

private String getKeyPressSequence(int[] pin){

    StringBuilder sb = new StringBuilder();
    int currRow =1;
    int currCol =1;

    for(int dest: pin){
        
        // Below is used to identify the row and colum of digit such as 
        // if dest = 5.. ( 5 - 1) / 3 + 1 = 2nd Row [ As we have 3 digits in every row]
        int destRow = (dest - 1) / 3 +1;
        int destCol = (dest - 1) % 3 +1;

        // Based on current position of Col and Row we move  L .. R preferring L/R first before 
        // going Down or U..
        while(currCol < destCol){
            sb.append('R');
            currCol++;
        }

        while(currCol > destCol){
            sb.append('L');
            currCol --;
        }

        while(currRow < destRow){
            sb.append('D');
            currRow++;
        }

        while(currRow > destRow){
            sb.append('U');
            currRow--;
        }
        sb.append('P');

    }
    return sb.toString();
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly