• + 0 comments
            int seaLevel = 0;
            int valleyCount = 0;
    
            for (char step : path.toCharArray()) {
    
                    if (step == 'U') {
    
                            seaLevel++;
    
                            if (seaLevel == 0) {
    
                                    valleyCount++;
                            }
                    } else {
    
                            seaLevel--;
                    }
            }
    
            return valleyCount;