• + 0 comments
    public static int countingValleys(int steps, String path) {
    // Write your code here
    String[] s=path.split("");
    int i=0;
    int k=0,l=0;
    int j=0;
    for(int p=0;p<s.length;p++){
        if(s[p].equalsIgnoreCase("u")){
            k=j;
            j=i;
            i++;
        }
        else{
            k=j;
            j=i;
            i--;
            if((k==1||k==0||k==-1)&&j==0&&i==-1){
                l++;
            }
    
    
        }
    }
    return l;
    
    }