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 in = new Scanner(System.in); /* Parse input */ int n = in.nextInt(); in.nextLine(); String steps = in.nextLine(); /* Close scanner */ in.close(); /* Calculate number of valleys */ int seaLevel = 0; //keep track of how far above or below sea level boolean inValley = false; //true when below sea level int numOfValleys = 0; //keep track of completed valleys for (int i = 0; i < steps.length(); i++) { if (steps.charAt(i) == 'U') seaLevel++; else seaLevel--; // Check if below sea level, if not there already if (!inValley && seaLevel < 0) inValley = true; // Check if position is in valley and valley has ended (back up to sea level) else if (inValley && seaLevel == 0) { inValley = false; numOfValleys++; } } System.out.println(numOfValleys); } }