Skip to content

Commit 76d86c7

Browse files
committed
Remove Consecutive Duplicates
1 parent 66943e7 commit 76d86c7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Data Structures and Algorithms Patterns implemented in Python.
1212
- [x] [Recursion on Arrays](Recursion/Arrays)
1313
- [x] [Merge Sort Array](Recursion/Arrays/mergesort.py)
1414
- [x] [Quick Sort Array](Recursion/Arrays/quicksort.py)
15+
- [x] [Recursion on Strings](Recursion/Strings)
16+
- [x] [Remove Consecutive Duplicates](Recursion/Strings/removeconsecutiveduplicates.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Given a string S, remove all the consecutive duplicates. Here we keep one character and remove all subsequent same characters.
3+
4+
Examples:
5+
6+
Input : aaaaabbbbbb
7+
Output : ab
8+
9+
Input : aabccba
10+
Output : abcba
11+
12+
'''
13+
14+
def removeconsecutivestring(Str):
15+
if len(Str)<2:
16+
return Str
17+
if Str[0]!=Str[1]:
18+
return Str[0]+removeconsecutivestring(Str[1:])
19+
return removeconsecutivestring(Str[1:])
20+
21+
print(removeconsecutivestring("aabccba")) #abcba

0 commit comments

Comments
 (0)