Created
September 21, 2020 17:40
-
-
Save avirajkhare00/d95cc83d2667983102f86b5f34a7b12b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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