/** * https://www.hackerrank.com/contests/rookierank/challenges/counting-valleys * All Contests RookieRank Counting Valleys */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Solution { public static void main(String[] args) throws NumberFormatException, IOException { FasterScanner sc = new FasterScanner(System.in); int nNumberSteps = sc.nextInt(); String steps = sc.nextLine(); int currElevation = 0; int valleyCount = 0; int state = 0; for (int i = 0; i < nNumberSteps; i++) { String s = steps.substring(i, i+1); if (s.compareTo("U") == 0) { currElevation++; } else { currElevation--; } if (state >= 0 && currElevation < 0) { valleyCount++; } state = currElevation; } System.out.println(valleyCount); sc.close(); } /** Buffered replacement for the java.util.Scanner */ static class FasterScanner { private static BufferedReader _reader; private static StringTokenizer _parser; public FasterScanner(InputStream input) { _reader = new BufferedReader(new InputStreamReader(input)); _parser = new StringTokenizer(""); } /** * Get the next string token from the buffer. */ public String next() throws IOException { while (!_parser.hasMoreTokens()) { _parser = new StringTokenizer(_reader.readLine()); } return _parser.nextToken(); } /** * Get the next line of text from the buffer. */ public String nextLine() throws IOException { try { return _reader.readLine(); } catch (IOException ex) { return null; } } /** * Get the next int from the buffer. */ public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } /** * Get the next long from the buffer. */ public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } /** * Close the scanner. */ public void close() throws IOException { _parser = null; if (_reader != null) { _reader.close(); _reader = null; } } } }