import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int numSteps = scanner.nextInt(); String steps = scanner.next(); int elevation = 0; boolean inValley = false; int numValleys = 0; for (int i = 0; i < numSteps; i++) { if (steps.charAt(i) == 'U') { // Detect if we are exiting a valley if (elevation == -1) { inValley = false; numValleys++; } elevation++; } else if (steps.charAt(i) == 'D') { // Detect if we are entering a valley if (elevation == 0) { inValley = true; } elevation--; } else { throw new Exception("Unknown step direction. Only allowed 'U' for up and 'D' for down. Got \"" + steps + "\""); } } System.out.println(numValleys); } }