Monday, July 23, 2018

Counting Valleys : Algorithms - Implementation

#The solution in Python3 is as follows:


def countingValleys(n, steps):
    seaLevel = valley = 0

    for step in steps:
        if step == 'U':
            seaLevel += 1
        else:
            seaLevel -= 1
       
        if step == 'U' and seaLevel == 0:
            valley += 1
   
    return valley
n=int(input())
s=input()
print(countingValleys(n,s))

#Keep visiting for more solutions of HackerRank problems in Python 3.Thank you!!!