Skip to content

Commit 9992d5c

Browse files
committed
Adding counting valleys algorithm, that deals with strings
1 parent 78d8a9b commit 9992d5c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Counting valleys deals with strings. One has to count the number of valleys and Hills in the given string and return the position of traveller at end w.r.t the start point.
3+
'U' is treated as 'the traveller came across a hill' and 'D' denotes that he came across a valley.
4+
5+
Input is guranteed as a string with 'U', and 'D' with an integer having the value of length of input string.
6+
7+
Output should be a single integer that denotes the number of valleys traveller walked through during his hike.
8+
9+
Example :
10+
Input = 8
11+
UDDDUDUU
12+
13+
Output = 1
14+
'''
15+
16+
import sys
17+
n = int(input())
18+
s = input()
19+
20+
m = 0
21+
v = 0
22+
23+
for i in s:
24+
if i == 'U':
25+
m += 1
26+
if m == 0:
27+
v += 1
28+
else:
29+
m -= 1
30+
print(v)
31+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy