import scala.math.random object Solution { // turns a list of the characters "U" and "D" into a list of heights, and then // tests for crossings from height 0 to height -1 def main(args: Array[String]) { var level = -1; val numSteps = readInt // initial "U" from height -1 blocks the edge case of starting with a // downward step. val numValleys = ("U" ++ readLine()) .map(v=>if (v == 'U') {level += 1; level} else {level -= 1; level}) .sliding(2) .filter(v=>(v(0) == 0 && v(1) < 0)) .map(_=>1) .sum println(numValleys) } }