function processData(input) { //Enter your code here input = input.split('\n'); n = input[0]; steps = input[1]; var valleys = 0; var height = 0; var inValley = false; for(let c = 0; c < n; c++){ if(steps[c] === 'U'){ height++; }else{ height--; } if(inValley && height == 0) valleys++; inValley = (height < 0); } console.log(valleys); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });