Skip to content

Instantly share code, notes, and snippets.

@zenius
Created July 10, 2020 17:13
Show Gist options
  • Save zenius/fce6fb49218cf16ed52845619533820f to your computer and use it in GitHub Desktop.
Save zenius/fce6fb49218cf16ed52845619533820f to your computer and use it in GitHub Desktop.
#Decompress String
You are given a string S. You to have to first sort the characters in the string.
Now, Suppose a character 'c' occurs x times in the modified string.
Replace these consecutive occurrences of the character 'c' with (x, c) in the string.
Itertools.groupby() will help you in achieving it.
Input Format
First line will contain a string s.
Constraints
2 <= |s| <= 10^3
Output Format
A single line containing the modified string.
Sample TestCase 1
Input
aaabbcccd
Output
(3, 'a') (2, 'b') (3, 'c') (1, 'd')
Solution:
def main():
lst = list(input())
lst.sort()
temp, count, length = lst[0], 0, len(lst)
for value in lst:
if temp != value:
print((count, temp), end = ' ')
temp, count = value, 1
else:
count += 1
print((count , lst[length - 1]), end = '')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment