repl.it
@haideralsh/

valleys

Go

No description

fork
loading
Files
  • main.go
main.go
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
package main

import (
  "fmt"
  )

func main() {
  fmt.Println(countingValleys("UDDDUDUU"))
}

func countingValleys(s string) int {
  var (
    level = 0
    insideValley bool = false
    valleys = 0
  )

  for _, c := range s {
    if c == 'U'  {
      level = level + 1

      if level >= 0 {
        insideValley = false
      }
    } else if c == 'D' {
      level = level - 1

      if level < 0 && insideValley == false {
        valleys = valleys + 1
        insideValley = true
      }
    }
  }

  return valleys
}