// number of elements var n = Int(readLine()!)! // read array and map the elements to integer var arr = Array(readLine()!.characters) // variable to hold the sum of the array elements var level = 0 var valleys = 0 for (var i = 0; i < n; i++) { // Are we starting a valley? if (arr[i] == "U") { level++ } if (arr[i] == "D") { level-- if (level == -1) { valleys++ } } } // print the array elements print(valleys)