Skip to content

Instantly share code, notes, and snippets.

@tuxskar
Last active August 29, 2015 14:12
Show Gist options
  • Save tuxskar/59ad252cf3a5acc5ead5 to your computer and use it in GitHub Desktop.
Save tuxskar/59ad252cf3a5acc5ead5 to your computer and use it in GitHub Desktop.
Script to remove duplicate chars in strings with O(n) complexity for each string
#!/usr/bin/env python
import argparse
def remove_duplicated(ss, verbose=False):
if verbose: print "initial " + ss
s = [x for x in ss]
duplicated = 0
for i in range(1, len(s)):
if s[i] == s[i-1]: duplicated += 1
s[i-duplicated] = s[i]
print "final " if verbose else "" + "".join(s[:len(s)-duplicated])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v',
action='store_true',
help='verbose flag')
parser.add_argument('string',
help='string with duplicated characters',
nargs='*')
args = parser.parse_args()
for s in args.strings:
remove_duplicated(s, args.verbose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment