function processData(input) { //Enter your code here var chunks = input.split('\n'); var stepCount = parseInt(chunks[0]); var steps = chunks[1]; var valleys = 0; var height = 0; for(var i = 0; i < stepCount; i++){ if(steps.charAt(i) == 'D'){ if(height == 0){ valleys++; } height++; } else { height--; } } 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); });