### Problem

Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly ** n** steps. For every step he took, he noted if it was an

*uphill*

**, or a**

*U**downhill*,

**step. Gary’s hikes start and end at sea level and each step up or down represents a**

*D***1**unit change in altitude. We define the following terms:

- A
*mountain*is a sequence of consecutive steps*above*sea level, starting with a step*up*from sea level and ending with a step*down*to sea level. - A
*valley*is a sequence of consecutive steps*below*sea level, starting with a step*down*from sea level and ending with a step*up*to sea level.

Given Gary’s sequence of *up* and *down* steps during his last hike, find and print the number of *valleys* he walked through.

For example, if Gary’s path is **8 = [DDUUUUDD]**, he first enters a valley **2** units deep. Then he climbs out an up onto a mountain ** 2** units high. Finally, he returns to sea level and ends his hike.

**Function Description**

Complete the *countingValleys* function in the editor below. It must return an integer that denotes the number of valleys Gary traversed.

countingValleys has the following parameter(s):

*n*: the number of steps Gary takes*s*: a string describing his path

**Input Format**

The first line contains an integer ** n**, the number of steps in Gary’s hike.

The second line contains a single string

**, of**

*8***characters that describe his path.**

*n***Constraints**

**Output Format**

Print a single integer that denotes the number of valleys Gary walked through during his hike.

**Sample Input**

```
8
UDDDUDUU
```

**Sample Output**

```
1
```

**Explanation**

If we represent `_`

as sea level, a step up as `/`

, and a step down as `\`

, Gary’s hike can be drawn as:

```
_/\ _
\ /
\/\/
```

He enters and leaves one valley.

### Solution

Ok, first of all let’s understand the terms they are using in this problem:

- A
*mountain*is a sequence of consecutive steps*above*sea level, starting with a step*up*from sea level and ending with a step*down*to sea level. - A
*valley*is a sequence of consecutive steps*below*sea level, starting with a step*down*from sea level and ending with a step*up*to sea level.

In the explanation section we clearly see one valley, it’s everything that happens below sea level, starting with a step down from sea level, and ending with a step up TO sea level.

So, we want to know where we are on each step, and the amount of valleys we went through, let’s define those variables first.

```
var valleyCount = 0
var currentLevel = 0
```

Let’s think about this for a moment, first of all, we start at the sea level, that makes us start at currentLevel 0, so going straight to D, will start a valley, but if the List starts with U, we need to know how many steps are left to get below sea level, that’s easy, let’s add/subtract ** 1** depending if the next step in the List is

**or**

*U***respectively.**

*D*```
s.forEach { step ->
if (step == 'U')
currentLevel++
else
currentLevel--
}
```

The inline function forEach already facilitates us a quick way to iterate over each Int in the list of steps, for each steps, we look for ** U** or

**(else), we know this are the only two possible values.**

*D*Now, we need to know when we get into a valley, that will happen when our currentLevel goes below zero, but also, we need to ensure to not count again when we are already in the valley, and we didn’t go over the sea level again. Let’s define a quick boolean to know that.

`var inValley = false`

In our iteration over the steps, let’s check, if we go below sea level and we are not in the valley, let’s sum up one valley to our valleyCount variable.

```
if (!inValley && currentLevel < 0) {
valleyCount++
inValley = true
}
```

We don’t really care what happens when we are already in the valley, so we need to wait until we get to the sea level again in order to let our code know it’s allowed to start another valley.

So let’s set inValley to false again, when we go above 0 in our currentLevel variable.

```
if (currentLevel >= 0)
inValley = false
```

Now, outside our forEach (of course), let’s return our valleyCount variable.

And, with this we are hopefully done, let’s first check running this code with sample input on our own environment.

```
8
UDDDUDUU
```

Running…

`1`

Let’s submit the code in HackerRank and see if we pass all test cases.

Hope this helped you understand a fair solution made in Kotlin for this problem, if you have any suggestion or a better way of doing this in the same language, don’t hesistate on commenting below!