import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner stdIn = new Scanner(System.in); int seaLevel = 0, numOfSteps = 0, i = 0, count = 0; int[] seaLevels = null; //Bug. If seaLevels is removed, no response from STDOUT. String steps = null; boolean outOfRange = false, potentialValley = false; if (stdIn.hasNextInt()) { numOfSteps = stdIn.nextInt(); if (numOfSteps >= 2 && numOfSteps <= Math.pow(10.0, 6.0)) { seaLevels = new int[numOfSteps]; if (stdIn.hasNext()) { steps = stdIn.next(); while (i < steps.length()) { switch (steps.charAt(i)) { case 'U': { seaLevel++; seaLevels[i++] = seaLevel; if (seaLevel == 0 && potentialValley) { count++; potentialValley = false; } break; } case 'D': { seaLevel--; potentialValley = true; seaLevels[i++] = seaLevel; break; } } } System.out.println(count); } } else outOfRange = true; } } }