• + 0 comments
    public static int countingValleys(int steps, String path) {
        // Write your code here
        
            int upCnt=0,downCnt=0,valley=0;
            for(Character c:path.toCharArray()){
                if(c=='U')upCnt++;
                else if(c=='D')downCnt++;
                if(upCnt-downCnt==0 && c=='U'){
                    valley++;
                }
            }
            
            return valley;
            
        }