Skip to content

Instantly share code, notes, and snippets.

@lucasdemarchi
Created July 13, 2018 23:11
Show Gist options
  • Save lucasdemarchi/9cb6affe8a0af0f60f3e55dd54ca580a to your computer and use it in GitHub Desktop.
Save lucasdemarchi/9cb6affe8a0af0f60f3e55dd54ca580a to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import sys
def f(s, k):
o = [0] * len(s)
missing = k
chars = list("abcdefghijklmnopqrstuvwxyz")
for c in chars:
for pos in reversed(range(0, len(s))):
if c == s[pos] and (o[pos] == 0 or missing > -o[pos]):
o[pos] = ord(c)
missing = missing - 1
if missing == 0:
return o
right_empty = 0
right_empty_new = 0
for pos in reversed(range(0, len(s))):
if o[pos] <= 0:
o[pos] = -right_empty
right_empty_new = right_empty_new + 1
else:
right_empty = right_empty_new
return o
s = list(sys.argv[1])
k = int(sys.argv[2])
o = f(s, k)
# O(n)
for x in o:
if x > 0:
print(chr(x), end='')
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment