• + 0 comments

    Maybe help you with my solution:

    static int countingValleys(int n, string s){

        int numSeaLevel = 0;
        int numValleys = 0;
    
        for(int i=0;i<n;i++){
            if(s[i] == 'U')
                numSeaLevel++;
    
            if(s[i] == 'D') 
                numSeaLevel--;
    
            if(numSeaLevel==0 && s[i] == 'U'){
                numValleys++;
            }
        }
        return numValleys;
    }