Counting Valleys

Easy · Counting Warm-ups · String · Simulation · Prefix Sum

Problem

A hiker starts at sea level and takes steps steps, each described by a character in path:

A valley is any maximal stretch of the walk that starts with a step *below* sea level and ends when the hiker returns *to* sea level. Return the number of valleys walked through.

Examples

Input: steps = 8, path = "UDDDUDUU"
Output: 1

The hiker dips below sea level once and comes back up once.

Input: steps = 8, path = "DDUUDDUU"
Output: 2

Two separate dips below sea level.

Constraints

Approaches

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…