Skip to content

Instantly share code, notes, and snippets.

@bhbmaster
Last active December 11, 2020 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhbmaster/b536960bdd0296769e1acd956806a686 to your computer and use it in GitHub Desktop.
Save bhbmaster/b536960bdd0296769e1acd956806a686 to your computer and use it in GitHub Desktop.
Google Interview Question 2 - Remove duplicates. Uniq on a string
#
#
# example input: aaaabbcaaa
# output: abca
#
#
# req: python 3.5+
#
#
def solve(string):
ans=""
cont=False
oldval="BLAH"
for i,val in enumerate(string):
# print(f"{i} {val}")
if val==oldval:
cont=True
else:
ans += val
cont=False
oldval=val
print(ans)
solve("abc") # abc
solve("aaaabbcaaa") # abca
solve("abczzz") # abcz
solve("assssssssssswassssddddddddssss")
solve("kooooooooossstttttttttttttttttiaaaaaaaaaa asdfasdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment