Counting Valleys

  • + 0 comments
    public static int countingValleys(int steps, String path) {
    // Write your code here
        int step =0;
        int count = 0;
        for(int i = 0; i < path.length(); i++){
            if(path.charAt(i) == 'U'){
                step++;
            }else if(path.charAt(i) == 'D'){
                if(step == 0){
                   count++; 
                }
                step--;
            }
        }
        return count;
    }