Counting Valleys

  • + 1 comment

    def countingValleys(steps, path):

    n = 0
    p = 0
    for i in path:
        if i == 'D':
            n -= 1
        else:
            n += 1
            if n == 0:
                p += 1
    return p