public class HackerRankValleys {

    private static void countingValleys(int n, String walk) {

        //Count number of times reaching above sea level
        int seaLevel = 0;
        int noOfValleys = 0;

        for(char c: walk.toCharArray()) {
            if(c== 'U') {
                seaLevel++;
            } else {
                seaLevel--;
            }
            //if we just came up to sea level:
            if ((seaLevel == 0) && c== 'U') {
                noOfValleys++;
            }
        }
        System.out.println("Valleys: " + noOfValleys);
    }

    public static void main(String[] args) {
        int n = 8;
        String s = "UDDDUDUU";
        countingValleys(n, s);
    }


}
