In this Leetcode Compress the String! problem solution You are given a string S. Suppose a character 'c' occurs consecutively X times in the string. Replace these consecutive occurrences of the character 'c' with (X,c) in the string.

HackerRank Compress the String! in python problem solution

 HackerRank Compress the String! in python problem solution

# Enter your code here. Read input from STDIN. Print output to STDOUT

s = raw_input()

new = True
curC = ''
cnt = 0

for i in s:
    if i == curC:
        cnt = cnt + 1
    else:
        if curC != '':
            print ('(%d, %s)' % (cnt, curC) ),
        curC = i
        cnt = 1
        
print ('(%d, %s)' % (cnt, curC) )