Bingu was testing all the strings he had at his place and found that most of them were prone to a vicious attack by Banju, his arch-enemy. Bingu decided to encrypt all the strings he had, by the following method. Every substring of identical letters is replaced by a single instance of that letter followed by the number of occurrences of that letter. Then, the string thus obtained is further encrypted by reversing it.
Example 1:
Input:
s = "aabc"
Output: 1c1b2a
Explanation: aabc
Step1: a2b1c1
Step2: 1c1b2a
Example 2:
Input:
s = "aaaaa"
Output: 5a
Explanation: aaaaa
Step 1: a5
Step 2: 5a
Your Task:
You don't need to read input or print anything.Your task is to complete the function encryptString() which takes a single argument(s) and returns the encrypted string. 
Expected Time Complexity: O(|s|).
Expected Auxiliary Space: O(|s|).
Constraints
1 <= |s| <=1000
|s| denotes the length of the string s

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

    def encryptString(self, s):
        # code here

Solutions:
class Solution:

	def encryptString(self, s):
		if len(s) < 2:
			return '1' + s
		s1 = ''
		c = 1
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				c += 1
			else:
				s1 += s[i - 1] + str(c)
				c = 1
		s1 += s[i] + str(c)
		return s1[::-1]


from collections import Counter

class Solution:

	def encryptString(self, s):
		n = len(s)
		e = ''
		i = 0
		while i < n:
			count = 1
			j = i + 1
			while j < n and s[j] == s[i]:
				count += 1
				j += 1
			e += s[i] + str(count)
			i = j
		return e[::-1]


class Solution:

	def encryptString(self, s):
		if len(s) < 2:
			return '1' + s[0]
		else:
			count = 1
			S = ''
			for i in range(1, len(s)):
				if s[i] == s[i - 1]:
					count += 1
				else:
					S += s[i - 1] + str(count)
					count = 1
			S += s[i] + str(count)
			return S[::-1]


class Solution:

	def encryptString(self, s):
		res = ''
		c = 0
		for i in range(len(s)):
			c += 1
			if i + 1 < len(s) and s[i] == s[i + 1]:
				continue
			res += s[i]
			res += str(c)
			c = 0
		return res[::-1]


class Solution:

	def encryptString(self, s):
		ans = ''
		stack = [s[0]]
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				stack.append(s[i])
			else:
				ans += s[i - 1] + str(len(stack))
				stack = [s[i]]
		if len(stack) != 0:
			ans += s[-1] + str(len(stack))
		return ans[::-1]


from collections import OrderedDict

class Solution:

	def encryptString(self, s):
		s1 = ''
		i = 0
		while i < len(s):
			s1 += s[i]
			c = 0
			while i < len(s) and s[i] == s1[-1]:
				c += 1
				i += 1
			s1 += str(c)
		return s1[::-1]


class Solution:

	def encryptString(self, s):
		i = 0
		res = ''
		while i < len(s):
			res += s[i]
			c = 0
			while i < len(s) and s[i] == res[-1]:
				c += 1
				i += 1
			res += str(c)
		return res[::-1]


class Solution:

	def encryptString(self, s):
		ans = ''
		count = 1
		for i in range(len(s) - 1):
			if s[i] != s[i + 1]:
				ans += s[i]
				ans += str(count)
				count = 1
			else:
				count += 1
		if count > 1:
			ans += s[-1]
			ans += str(count)
		else:
			ans += s[-1]
			ans += str(count)
		return ans[::-1]


class Solution:

	def encryptString(self, s):
		result = ''
		i = 0
		while i < len(s):
			count = 1
			j = i + 1
			while j < len(s) and s[j] == s[i]:
				j += 1
				count += 1
			result += s[i] + str(count)
			i = j
		result = result[::-1]
		return result


class Solution:

	def encryptString(self, s):
		n = len(s)
		t = 1
		res = ''
		for i in range(n - 1):
			if s[i] == s[i + 1]:
				t += 1
			else:
				res += s[i] + str(t)
				t = 1
		res += s[-1] + str(t)
		return res[::-1]


class Solution:

	def encryptString(self, s):
		str1 = ''
		count = 1
		temp = s[0]
		i = 1
		while i < len(s):
			if s[i] != temp:
				str1 += temp + str(count)
				temp = s[i]
				count = 1
				i += 1
			else:
				count += 1
				i += 1
		str1 += temp + str(count)
		str1 = str1[::-1]
		return str1


class Solution:

	def encryptString(self, s):
		h = {}
		track = ''
		result = ''
		for i in s:
			if i not in h:
				h[i] = 1
			else:
				h[i] += 1
			if len(track) > 0 and track != i:
				ch = track + str(h[track])
				result += ch
				del h[track]
			track = i
		result += track + str(h[track])
		return result[::-1]


class Solution:

	def encryptString(self, s):
		ls = list(s)
		prv = ls[0]
		count = 0
		st = ''
		for i in range(1, len(ls)):
			if ls[i] == prv:
				count += 1
			else:
				st = st + prv + str(count + 1)
				count = 0
			prv = ls[i]
		st = st + prv + str(count + 1)
		return st[::-1]


class Solution:

	def encryptString(self, s):
		i = 0
		d = ''
		c = 1
		for j in range(len(s) - 1):
			if s[i] == s[j + 1]:
				i = i + 1
				c = c + 1
			else:
				d = d + s[i]
				d = d + str(c)
				i = i + 1
				c = 1
		if c >= 1:
			d = d + s[i]
			d = d + str(c)
			i = i + 1
			c = 1
		return d[::-1]


class Solution:

	def encryptString(self, s):
		x = s[0]
		c = 1
		z = ''
		for j in range(1, len(s)):
			if x == s[j]:
				c += 1
			else:
				z += x + str(c)
				c = 1
				x = s[j]
		z += x + str(c)
		return z[::-1]


class Solution:

	def encryptString(self, s):
		result = s[0]
		prev = s[0]
		count = 1
		for i in range(1, len(s)):
			if prev == s[i]:
				count += 1
			else:
				result += str(count) + s[i]
				count = 1
				prev = s[i]
		result += str(count)
		return result[::-1]


class Solution:

	def encryptString(self, s):
		result = ''
		new = s[0]
		for i in s[1:]:
			if new[-1] == i:
				new += i
			else:
				result += new[0] + str(len(new))
				new = i
		result += new[0] + str(len(new))
		return result[::-1]


class Solution:

	def encryptString(self, s):
		s1 = ''
		k = 0
		n = len(s)
		i = n
		for i in range(n - 1):
			if s[i] != s[i + 1]:
				if i < n - 2:
					s1 += s[i] + str(k + 1)
				else:
					s1 += s[i] + str(k + 1) + s[i + 1] + '1'
				k = 0
			else:
				k += 1
				if i == n - 2:
					s1 += s[i] + str(k + 1)
			i -= 1
		return s1[::-1] if n > 1 else '1' + s


class Solution:

	def encryptString(self, s):
		i = 0
		out = ''
		while i < len(s):
			count = 1
			while i < len(s) - 1 and s[i] == s[i + 1]:
				count += 1
				i += 1
			i += 1
			out += s[i - 1] + str(count)
		return out[::-1]


class Solution:

	def encryptString(self, s):
		l = []
		k = 1
		if len(s) > 1:
			for i in range(len(s) - 1):
				if s[i] == s[i + 1]:
					k += 1
					continue
				l.append(s[i] + str(k))
				k = 1
			if s[-1] != s[-2]:
				l.append(s[-1] + str(1))
			else:
				l.append(s[-1] + str(k))
			return ''.join(l)[::-1]
		elif len(s) == 1:
			return str(1) + s[0]


class Solution:

	def encryptString(self, s):
		new = ''
		count = 1
		l = 1
		r = len(s)
		while l < r:
			if s[l] == s[l - 1]:
				count += 1
				l += 1
			else:
				new += s[l - 1] + str(count)
				count = 1
				l += 1
		new += s[l - 1] + str(count)
		new = list(new)
		a = 0
		b = len(new) - 1
		while a < b:
			(new[a], new[b]) = (new[b], new[a])
			a += 1
			b -= 1
		return ''.join(new)


class Solution:

	def encryptString(self, s):
		a = 1
		s1 = ''
		for i in range(len(s)):
			if i == len(s) - 1:
				break
			if s[i] == s[i + 1]:
				a += 1
			elif a == 1:
				s1 += s[i] + '1'
			else:
				s1 = s1 + s[i] + str(a)
				a = 1
		if a == 1:
			s1 += s[i] + '1'
		else:
			s1 = s1 + s[i] + str(a)
		return s1[::-1]


class Solution:

	def encryptString(self, s):
		count = [s[0]]
		k = ''
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				count.append(s[i])
			else:
				k += s[i - 1] + str(len(count))
				count = [s[i]]
		if len(count) != 0:
			k += s[-1] + str(len(count))
		return k[::-1]


class Solution:

	def encryptString(self, s):
		m = len(s)
		k = 0
		l = [['' for i in range(2)] for j in range(len(s))]
		s1 = ''
		s2 = ''
		n = 0
		c = 1
		for i in range(m - 1):
			if s[i] == s[i + 1]:
				c += 1
				n = c
			else:
				l[k][0] = s[i]
				l[k][1] = c
				c = 1
			k += 1
		if s[m - 1] != s[m - 2]:
			l[k][0] = s[m - 1]
			l[k][1] = 1
		if s[m - 1] == s[m - 2]:
			l[k][0] = s[m - 1]
			l[k][1] = n
		if m == 1:
			l[0][0] = s[0]
			l[0][1] = 1
		for i in range(len(l)):
			s1 += str(l[i][0])
			s1 += str(l[i][1])
		l1 = list(s1)
		for j in l1[::-1]:
			s2 += j
		return s2


class Solution:

	def encryptString(self, s):
		num = 1
		ps = s[0]
		res = ''
		for i in range(1, len(s)):
			if s[i] == ps:
				num += 1
			else:
				res += ps + str(num)
				num = 1
			ps = s[i]
		res += ps + str(num)
		return res[::-1]
		return res


class Solution:

	def encryptString(self, s):
		from itertools import groupby
		d = []
		v = [[i, len(list(j))] for (i, j) in groupby(s)]
		for i in v:
			for j in i:
				d.append(j)
		h = ''.join(map(str, d))
		h = h[::-1]
		return h


class Solution:

	def encryptString(self, s):
		l = {}
		t = ''
		r = ''
		for i in s:
			if i not in l:
				l[i] = 1
			else:
				l[i] += 1
			if len(t) > 0 and t != i:
				c = t + str(l[t])
				r += c
				del l[t]
			t = i
		r += t + str(l[t])
		return r[::-1]


class Solution:

	def encryptString(self, s):
		save_in = {}
		count = 1
		encrypted_string = ''
		name_char = s[0]
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				count += 1
			else:
				encrypted_string += name_char + str(count)
				count = 1
				name_char = s[i]
		encrypted_string += name_char + str(count)
		return encrypted_string[::-1]


from collections import Counter

class Solution:

	def encryptString(self, s):
		ans = ''
		stack = [s[0]]
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				stack.append(s[i])
			else:
				ans += s[i - 1] + str(len(stack))
				stack = [s[i]]
		if len(stack) != 0:
			ans += s[-1] + str(len(stack))
		return ans[::-1]


class Solution:

	def encryptString(self, s):
		i = 0
		n = len(s)
		r = ''
		while i < n:
			temp = s[i]
			c = 1
			while i + 1 < n and s[i + 1] == temp:
				c += 1
				i += 1
			r += temp + str(c)
			i += 1
		return r[::-1]


class Solution:

	def encryptString(self, s):
		cipher = ''
		i = 0
		while i < len(s):
			cipher += s[i]
			count = 1
			j = i + 1
			for j in range(i + 1, len(s)):
				if s[j] == s[i]:
					count += 1
				else:
					break
			cipher += str(count)
			if j == len(s) - 1 and s[-1] == s[-2]:
				break
			i = j
		cipher = cipher[::-1]
		return cipher


class Solution:

	def encryptString(self, s):
		count = 1
		ans = ''
		for i in range(len(s) - 1):
			if s[i] == s[i + 1]:
				count += 1
			else:
				ans += s[i] + str(count)
				count = 1
		ans += s[-1] + str(count)
		return ans[::-1]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


class Solution:

	def encryptString(self, s):
		r = ''
		n = len(s)
		l = 0
		for i in range(n):
			l += 1
			if i + 1 < n and s[i] == s[i + 1]:
				continue
			r += s[i]
			r += str(l)
			l = 0
		p = r[::-1]
		return p
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


class Solution:

	def encryptString(self, s):
		str1 = ''
		count = 1
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				count += 1
				continue
			else:
				str1 += s[i - 1]
				str1 += str(count)
				count = 1
				continue
		str1 += s[len(s) - 1]
		str1 += str(count)
		output = ''
		i = len(str1) - 1
		while i >= 0:
			output += str1[i]
			i -= 1
		return output
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


import collections

class Solution:

	def encryptString(self, s):
		t = ''
		summ = 1
		for i in range(len(s)):
			if i < len(s) - 1:
				if s[i] == s[i + 1]:
					summ += 1
				else:
					t += s[i] + str(summ)
					summ = 1
			else:
				t += s[i] + str(summ)
				summ = 1
		txt = t[::-1]
		return txt
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


from collections import OrderedDict

class Solution:

	def encryptString(self, s):
		s1 = ''
		ct = 1
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				ct += 1
			else:
				s1 += s[i - 1] + str(ct)
				ct = 1
		s1 += s[-1] + str(ct)
		return s1[::-1]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


class Solution:

	def reverse(self, s):
		t = [*s]
		i = 0
		j = len(t) - 1
		while i < j:
			(t[i], t[j]) = (t[j], t[i])
			i += 1
			j -= 1
		return ''.join(t)

	def encryptString(self, s):
		temp = ''
		count = 0
		for i in range(len(s)):
			count += 1
			if i + 1 < len(s) and s[i] == s[i + 1]:
				continue
			temp += s[i]
			temp += str(count)
			count = 0
		return self.reverse(temp)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


class Solution:

	def encryptString(self, s):
		lst = list(s)
		count = 1
		res = s[0]
		for itr in range(len(lst) - 1):
			if lst[itr] == lst[itr + 1]:
				count += 1
			else:
				res += str(count)
				res += lst[itr + 1]
				count = 1
		res += str(count)
		return res[::-1]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


class Solution:

	def encryptString(self, s):
		count = [0] * 26
		basic_string = ''
		chars = []
		for i in range(len(s)):
			pos = ord(s[i]) - ord('a')
			count[pos] += 1
			if i < len(s) - 1 and s[i] == s[i + 1]:
				continue
			basic_string += s[i] + str(count[pos])
			count[pos] = 0
		return basic_string[::-1]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


from queue import deque

class Solution:

	def encryptString(self, s):
		dq = deque(s)
		pr = dq.popleft()
		ct = 1
		rslt = ''
		if len(s) == 1:
			rslt = rslt + pr + str(ct)
		else:
			while len(dq) > 0:
				cr = dq.popleft()
				if pr == cr:
					ct = ct + 1
				else:
					rslt = rslt + pr + str(ct)
					pr = cr
					ct = 1
				if len(dq) == 0:
					rslt = rslt + pr + str(ct)
					break
		return rslt[::-1]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


class Solution:

	def encryptString(self, s):
		ans = ''
		k = s[0]
		c = 0
		for i in s:
			if i == k:
				c += 1
			else:
				ans += k + str(c)
				k = i
				c = 1
		else:
			ans += k + str(c)
		return ''.join(reversed(ans))
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		s = input()
		solObj = Solution()
		print(solObj.encryptString(s))


class Solution:

	def encryptString(self, s):
		c = 1
		l = []
		i = 0
		while i < len(s):
			j = i + 1
			while j < len(s):
				if s[i] == s[j]:
					c += 1
					i += 1
				else:
					l.append(s[i])
					l.append(str(c))
					c = 1
					i = j - 1
					break
				j += 1
			i += 1
		l.append(s[i - 1])
		l.append(str(c))
		res = ''.join(l)
		return res[::-1]


class Solution:

	def encryptString(self, s):
		count = 1
		char = s[0]
		string1 = ''
		for i in range(1, len(s)):
			if s[i] == s[i - 1]:
				count += 1
			else:
				string1 += s[i - 1]
				string1 += str(count)
				count = 1
		string1 += s[len(s) - 1]
		string1 += str(count)
		return string1[::-1]


class Solution:

	def encryptString(self, s):
		updated = ''
		count = 1
		candidate = s[0]
		for character in s[1:]:
			if character == candidate:
				count += 1
			else:
				updated += candidate + str(count)
				candidate = character
				count = 1
		if count > 0:
			updated += candidate + str(count)
		return updated[::-1]


class Solution:

	def encryptString(self, s):
		if len(s) == 1:
			return '1' + s
		k = 1
		z = []
		for i in range(len(s) - 1):
			if s[i] == s[i + 1]:
				k += 1
			else:
				z.append((s[i] + str(k))[::-1])
				k = 1
			if i == len(s) - 2:
				z.append((s[i + 1] + str(k))[::-1])
		z.reverse()
		return ''.join(z)


class Solution:

	def encryptString(self, s):
		ans = ''
		c = ''
		for i in s:
			if len(c) < 1:
				c += i
			elif c[-1] != i:
				ans += c[0]
				ans += str(len(c))
				c = i
			else:
				c += i
		ans += c[0]
		ans += str(len(c))
		return ans[::-1]


class Solution:

	def encryptString(self, s):
		count = 1
		l = ''
		for i in range(len(s)):
			if i + 1 < len(s) and s[i] == s[i + 1]:
				count += 1
				continue
			l += s[i]
			l += str(count)
			count = 1
		return l[::-1]


class Solution:

	def encryptString(self, s):
		a = ''
		c = 1
		for i in range(1, len(s)):
			if s[i - 1] == s[i]:
				c = c + 1
			elif s[i - 1] != s[i]:
				a = a + s[i - 1]
				a = a + str(c)
				c = 1
		a = a + s[len(s) - 1]
		a = a + str(c)
		return a[::-1]


class Solution:

	def encryptString(self, s):
		ret = ''
		count = 0
		cur_char = s[0]
		for c in s:
			if c != cur_char:
				ret += cur_char + str(count)
				cur_char = c
				count = 0
			count += 1
		ret += cur_char + str(count)
		return ret[::-1]


class Solution:

	def encryptString(self, s):
		ans = ''
		last = s[0]
		c = 1
		for ch in s[1:]:
			if ch == last:
				c += 1
				last = ch
			else:
				ans += last + str(c)
				last = ch
				c = 1
		ans += last + str(c)
		return ans[::-1]