Problem · String

Final Position from Direction Commands

Learn this problem
EasyGoldman Sachs logoGoldman SachsFULLTIMEPHONE SCREEN

Problem statement

Start at coordinate (0, 0) and process commands from left to right. The letters L, R, U, and D move one unit left, right, up, and down. Lowercase forms have the same meaning. Ignore every other character.

Return [x, y] after all commands.

Function

finalCoordinates(commands: String) → int[]

Examples

Example 1

commands = "LRUDx"return = [0,0]

The four moves cancel and x is ignored.

Example 2

commands = "RRuLdd?"return = [1,-1]

Commands are case-insensitive and ? is ignored.

Constraints

  • 0 <= commands.length <= 1000000
  • commands contains ASCII characters.

More Goldman Sachs problems

drafts saved locally
public int[] finalCoordinates(String commands) {
  // write your code here
}
commands"LRUDx"
expected[0,0]
checking account