object Solution { def main(args: Array[String]) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ val numberOfSteps = readInt() val steps: String = readLine() def CountingValleys(numberOfSteps: Int, steps: String){ var noOfValleys: Int = 0 var seeLevel: Int = 0 var valley: Boolean = false for(c <- 0 until numberOfSteps){ if(steps(c) == 'U') { seeLevel += 1; checkAndCountValley() }else if(steps(c) == 'D') { seeLevel -= 1; checkAndCountValley() } } def checkAndCountValley() = { if(valley && seeLevel == 0){ noOfValleys += 1; valley = false }else if(seeLevel < 0 && (valley == false)) valley = true } println(noOfValleys) } CountingValleys(numberOfSteps, steps) } }