HackerRank Solution: Counting Valleys
(IN PYTHON 3)
import math
import os
import random
import re
import sys
# Complete the countingValleys function below.
def countingValleys(n, s):
c = v = 0
for i in s:
if(c == -1 and i == ‘U’):
v +=1
if(i == ‘D’):
c -=1
else:
c +=1
return v
if __name__ == ‘__main__’:
fptr = open(os.environ[‘OUTPUT_PATH’], ‘w’)
n = int(input())
s = input()
result = countingValleys(n, s)
fptr.write(str(result) + ‘\n’)
fptr.close()