Skip to content

Instantly share code, notes, and snippets.

@avirajkhare00
Created September 21, 2020 17:40
Show Gist options
  • Save avirajkhare00/d95cc83d2667983102f86b5f34a7b12b to your computer and use it in GitHub Desktop.
Save avirajkhare00/d95cc83d2667983102f86b5f34a7b12b to your computer and use it in GitHub Desktop.
def getLargestString(word, k):
countArr = [0]*26
a, ans = ord('a'), []
for c in word:
countArr[ord(c)-a] += 1
i = 25
while i >= 0:
if countArr[i] > k:
letter = chr(i+a)
ans.append(letter*k)
countArr[i] -= k
j = i-1
while(countArr[j] <= 0 and j>0):
j -= 1
if countArr[j] > 0 and j >= 0:
letter = chr(j+a)
ans.append(letter)
countArr[j] -= 1
else:
break
elif countArr[i] > 0:
letter = chr(i+a)
ans.append(letter*countArr[i])
countArr[i] = 0
else:
i -= 1
return ''.join(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment