using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ int n = Int32.Parse(Console.ReadLine()); string steps = Console.ReadLine(); int level = 0, valleyCount = 0; string lastDirection = "", currentDirection =""; bool startedAtSeaLevel = true; for (int i = 0; i < steps.Length; i++){ currentDirection = steps[i].ToString(); switch (currentDirection){ case "U": level ++; break; case "D": level --; break; } if (lastDirection != ""){ if (lastDirection != currentDirection){ if (startedAtSeaLevel && lastDirection == "D") valleyCount++; startedAtSeaLevel = false; } } lastDirection = currentDirection; if (level == 0){ startedAtSeaLevel = true; lastDirection = ""; } // Console.WriteLine(i + " : " + currentDirection + " : " + level + " : " + valleyCount + " : " + startedAtSeaLevel); } Console.WriteLine(valleyCount); } }