Skip to content

Instantly share code, notes, and snippets.

@gauravbansal74
Created July 9, 2019 13:27
Show Gist options
  • Save gauravbansal74/70b05f6128c8c8cbff8b486c7502b2ea to your computer and use it in GitHub Desktop.
Save gauravbansal74/70b05f6128c8c8cbff8b486c7502b2ea to your computer and use it in GitHub Desktop.
Counting Valleys || HackerRank
// 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