Created
July 9, 2019 13:27
-
-
Save gauravbansal74/70b05f6128c8c8cbff8b486c7502b2ea to your computer and use it in GitHub Desktop.
Counting Valleys || HackerRank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Complete the countingValleys function below. | |
static int countingValleys(int n, String s) { | |
int noOfValleys = 0; | |
int currentLevel = 0; | |
for(char c: s.toCharArray()){ | |
if(c == 'U') ++currentLevel; | |
if(c == 'D') --currentLevel; | |
if(currentLevel == 0 && c == 'U') ++noOfValleys; | |
} | |
return noOfValleys; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment