Given a string like:
“aabbdeeeef”
or
“bbbbbbb”
we want to output:
“abdef”
and
“b”
respectively.
here are a few ways to do it in Python:
As a function:
Example 1
def RemoveAdjacentLetters(str): a="" for s in str: a+=s if s not in a else "" return a
Example 2
def RemoveAdjacentLetters(str): a="" for s in str: a+=("", num)[num not in a] return a
Example 3
def RemoveAdjacentLetters(str): a="" for s in str: if num not in a: a+=num return a
Example 4 – regular expression
def RemoveAdjacentLetters(n): return re.sub('(.)\1+', '\1', n)
Go read and learn more:
– Python Regular Expressions – Text Munging