function processData(input) { //Enter your code here var level = 0; var valleys = 0; var steps = [0]; for (i=0;i<_input.length;i++) { if (input[i] == 'U') { level++; } else if (input[i] == 'D') { level--; } steps.push(level); } for (i=1;i<=_input.length;i++) { if (steps[i] == 0) { if (steps[i-1] < 0) { valleys++; } } } 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); });