-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution7.java
50 lines (42 loc) · 1.16 KB
/
Solution7.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
Counting Valleys - https://www.hackerrank.com/challenges/counting-valleys/problem
*/
import java.io.*;
import java.util.*;
public class Solution7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String hike = in.next();
boolean atSeaLevel = true;
boolean valley = false;
int upCount = 0;
int downCount = 0;
int valleys = 0;
for( int i = 0 ; i < N; i++){
if('U' == hike.charAt(i)){
upCount++;
}
else{
if(atSeaLevel){
valley = true;
}
downCount++;
}
if(upCount == downCount){
if(valley){
valleys++;
}
atSeaLevel = true;
valley = false;
upCount = 0;
downCount = 0;
}
else{
atSeaLevel = false;
}
}
System.out.println(valleys);
in.close();
}
}