import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // number of steps Gary took (from user input) int steps = scan.nextInt(); // get "topography" string from input String topography = new String(scan.next()); // debug /* System.out.println("Steps: " + steps); System.out.println("Topography: " + topography); */ int elevation = 0; int valleys = 0; // loop through string for (int i = 0; i < topography.length(); i++) { // get current char (up or down) if (topography.charAt(i) == 'U') { elevation += 1; } else if (topography.charAt(i) == 'D') { elevation -=1; } // check if below sealevel and going down (entering a valley) if (elevation == -1 && (topography.charAt(i) == 'D')) { valleys += 1; } // assume all valleys will be exited (because Gary's hike ends at sealevel) } // System.out.print("Number of valleys: "); System.out.println(valleys); } }