function processData(input) { var splitted = input.split('\n'); var n = +splitted[0]; var arr = splitted[1].trim(); var count = 0; var Ucount = 0; var level = 0; var previousLevel = 0 var above = false; for (var i = 0; i < n; i++){ if(arr[i] === 'U'){ level++; } else { level--; } if (level>0){ above = true; }else if(level <0){ above = false }else if (level === 0 && !above){ count ++; } } process.stdout.write(count); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });