Robot Final Position
JavaScript
Medium
4 views
Problem Description
You get a string of commands with letters L,R,U,D. Start at (0,0). Print final x y.
Input Format
One line command string.
Output Format
Two integers: x y.
Constraints
Length up to 2e5.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();let x=0,y=0;for(const ch of s){if(ch==='L')x--;else if(ch==='R')x++;else if(ch==='U')y++;else if(ch==='D')y--;}process.stdout.write(x+' '+y);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!