• + 0 comments
    public static int countingValleys(int steps, String path) {
    // Write your code here
    int ans = 0,level=0;
    for(char c: path.toCharArray()) {
        if(c=='U') level+=1;
        else level-=1;
        if(level==0 && c=='U') ans++;
    }
    return ans;
    

    } `