#include <stdio.h>
int countingValleys(int n, char* s) {
int level = 0, count = 0;
for(int i = 0; s[i]; i++) {
int prevLevel = level;
if (s[i] == 'U') {
level ++;
} else {
level --;
}
if (level < 0 && prevLevel >= 0) {
count ++;
}
}
return count;
}
int main()
{
int n;
char str[1000001];
scanf("%d", &n);
scanf("%s", str);
printf("%d\n", countingValleys(n, str));
return 0;
}
System Design for Beginners
A masterclass that helps early engineers and product managers become great at designing scalable systems.
132+ learners
Details →System Design Masterclass
A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.
1000+ learners
Details →Redis Internals
Learn internals of Redis by re-implementing some of the core features in Golang.
98+ learners
Details →