You are given the string S . Compress the string when lower and upper cases are the same. In compressed string characters should be in lowercase.
Example 1:
Input: S = "aaABBb"
Output: "3a3b"
Explanation: As 'a' appears 3 times
consecutively and 'b' also 3 times,
and 'b' and 'B' considered as same. 
Ã¢â¬â¹Example 2:
Input: S = "aaacca"
Output: "3a2c1a"
Explanation: As 'a' appears 3 times
consecutively and 'c' also 2 times,
and then 'a' 1 time.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function transform() which takes the string S as inputs and returns the compressed string.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 2 * 10^{5}
S contains only lowercase and uppercase characters.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

    def transform(self, S):
        # code here

Solutions:
class Solution:

	def transform(self, S):
		ans = ''
		last = ''
		cnt = ''
		for i in range(len(S)):
			curr = S[i].lower()
			if curr == last:
				cnt += 1
			else:
				ans += str(cnt) + last
				last = curr
				cnt = 1
		ans += str(cnt) + last
		return ans


class Solution:

	def transform(self, S):
		s = S.lower()
		s1 = ''
		c = s[0]
		cou = 0
		i = -1
		for i in range(len(s) - 1):
			if s[i] == c:
				cou += 1
			elif s[i] != c:
				s1 += str(cou)
				s1 += c
				cou = 1
			c = s[i]
		if c == s[i + 1]:
			cou += 1
			s1 += str(cou)
			s1 += c
		else:
			s1 += str(cou)
			s1 += c
			s1 += '1' + s[i + 1]
		return s1


class Solution:

	def transform(self, s):
		s = s.lower()
		ans = ''
		c = 1
		x = s[0]
		for i in s[1:]:
			if i == x:
				c += 1
			else:
				ans = ans + str(c) + x
				c = 1
				x = i
		ans = ans + str(c) + x
		return ans


class Solution:

	def transform(self, s):
		s = s.lower()
		n = len(s)
		pre_char = s[0]
		count = 1
		final = ''
		for i in range(1, n):
			if pre_char == s[i]:
				count += 1
			else:
				final += str(count) + s[i - 1]
				count = 1
				pre_char = s[i]
		return final + str(count) + s[n - 1]


class Solution:

	def transform(self, S):
		st = []
		s = ''
		S = S.lower()
		for i in S:
			if not st:
				st.append(i)
			elif st[-1] == i:
				st.append(i)
			else:
				c = 0
				while st:
					m = st.pop()
					c += 1
				s = s + str(c) + m
				st.append(i)
		c = 0
		while st:
			c += 1
			m = st.pop()
		s = s + str(c) + m
		return s


class Solution:

	def transform(self, S):
		S = S.lower()
		k = ''
		l = []
		l.append(S[0])
		for i in S[1:]:
			if l[0] == i:
				l.append(i)
			else:
				k = k + str(len(l)) + l[0]
				l.clear()
				l.append(i)
		return k + str(len(l)) + l[0]


class Solution:

	def transform(self, S):
		S = S.lower()
		string = ''
		count = 1
		temp = S[0]
		for char in S[1:]:
			if char == temp:
				count += 1
			else:
				string += str(count) + temp
				count = 1
				temp = char
		string += str(count) + temp
		return string


class Solution:

	def transform(self, s):
		if not s:
			return ''
		s = s.lower()
		pointer = s[0]
		count = 1
		result = ''
		for val in s[1:]:
			if pointer == val:
				count += 1
			else:
				result += str(count) + pointer
				pointer = val
				count = 1
		result = result + (str(count) + pointer)
		return result


class Solution:

	def transform(self, S):
		x = S.lower()
		string = ''
		count = 0
		temp = x[0]
		for i in range(len(S)):
			if x[i] == temp:
				count = count + 1
			else:
				string = string + str(count)
				string = string + temp
				count = 1
				temp = x[i]
		return string + str(count) + temp


class Solution:

	def transform(self, S):
		s = S.lower()
		output = ''
		count = 1
		pre = s[0]
		if len(s) == 1:
			output = output + str(count) + pre
		for i in range(1, len(s)):
			if s[i] == pre:
				count += 1
			else:
				output = output + str(count) + pre
				pre = s[i]
				count = 1
			if i == len(s) - 1:
				output = output + str(count) + pre
		return output


class Solution:

	def transform(self, s):
		s = s.lower()
		result = ''
		item = s[0]
		count = 1
		for char in s[1:]:
			if char == item:
				count += 1
			else:
				result += str(count) + item
				count = 1
				item = char
		result += str(count) + item
		return result


class Solution:

	def transform(self, S):
		stk = []
		r = ''
		i = 0
		l = len(S)
		while i < l:
			t = S[i].lower()
			if not stk:
				stk.append(t)
				i += 1
			elif stk[-1] == t:
				stk.append(t)
				i += 1
			else:
				r += str(len(stk)) + stk[-1]
				stk = []
		r += str(len(stk)) + stk[-1]
		return r


class Solution:

	def transform(self, S):
		result = ''
		count = 1
		first = S[0].lower()
		for i in range(1, len(S)):
			if S[i].lower() == first:
				count += 1
			else:
				result += str(count) + first
				first = S[i].lower()
				count = 1
			if i == len(S) - 1:
				result += str(count) + first
		if len(S) == 1:
			result = str(count) + S[0].lower()
		return result


class Solution:

	def transform(self, s):
		c = 1
		t = s[0].lower()
		j = ''
		for i in range(1, len(s)):
			if t == s[i].lower():
				c += 1
			else:
				j += str(c) + t.lower()
				c = 1
				t = s[i].lower()
		j += str(c) + t.lower()
		return j


class Solution:

	def transform(self, S):
		c = 0
		s = ''
		co = 0
		for i in range(len(S) - 1):
			if S[i + 1].lower() != S[c].lower():
				s += str(co + 1)
				s += str(S[c].lower())
				co = 0
				c = i + 1
			else:
				co += 1
		s += str(co + 1)
		s += str(S[-1].lower())
		return s


class Solution:

	def transform(self, S):
		S = S.lower()
		final = ''
		rep = ''
		cnt = 0
		for s in S:
			if rep == '':
				rep = s
				cnt += 1
			elif rep == s:
				cnt += 1
			else:
				final += str(cnt) + rep
				cnt = 1
				rep = s
		final += str(cnt) + rep
		return final


class Solution:

	def transform(self, S):
		out = ''
		S = S.lower()
		i = 0
		while i < len(S):
			count = 1
			while i < len(S) - 1 and S[i] == S[i + 1]:
				count += 1
				i += 1
			i += 1
			out += str(count) + S[i - 1]
		return out


class Solution:

	def transform(self, S):
		c = 1
		aux = S[0].lower()
		s1 = ''
		for i in range(1, len(S)):
			if S[i].lower() == aux:
				c += 1
			else:
				s1 += str(c) + aux
				aux = S[i].lower()
				c = 1
		s1 += str(c) + aux
		return s1


class Solution:

	def transform(self, S):
		sta = []
		S = S.lower()
		st = ''
		k = 0
		for i in range(len(S)):
			if len(sta) == 0 or S[i] != sta[-1]:
				if k != 0:
					st += str(k) + sta.pop()
				sta.append(S[i])
				k = 1
			else:
				k += 1
			if len(S) - 1 == i:
				st += str(k) + sta.pop()
		return st


class Solution:

	def transform(self, S):
		x = S.lower()
		ans = ''
		t = x[0]
		cnt = 0
		N = len(S)
		for i in range(N):
			if x[i] == t:
				cnt += 1
			else:
				ans += str(cnt) + t
				cnt = 1
				t = x[i]
		return ans + str(cnt) + t


class Solution:

	def transform(self, S):
		S = S.lower()
		final_str = ''
		char_count = 0
		for i in range(len(S)):
			if i + 1 == len(S):
				if char_count != 0:
					char_count = char_count + 1
					final_str = final_str + str(char_count) + S[i]
				else:
					final_str = final_str + str(1) + S[i]
			elif S[i] == S[i + 1]:
				char_count = char_count + 1
			else:
				char_count = char_count + 1
				final_str = final_str + str(char_count) + S[i]
				char_count = 0
		return final_str


class Solution:

	def transform(self, S):
		S = S.lower()
		res = ''
		char = S[0]
		count = 1
		for i in range(1, len(S)):
			if S[i] == char:
				count += 1
			else:
				res = res + str(count) + char
				count = 1
				char = S[i]
		res = res + str(count) + char
		return res


class Solution:

	def transform(self, S):
		i = 0
		j = 0
		res = ''
		S = S.lower()
		while i < len(S):
			c = 0
			while j < len(S) and S[i] == S[j]:
				c += 1
				j += 1
			if c > 0:
				res += str(c) + S[i]
			i += 1
		return res


class Solution:

	def transform(self, S):
		k = 0
		i = 0
		j = 0
		li = []
		while j < len(S) and i <= j:
			if ord(S[i]) == ord(S[j]) or abs(ord(S[i]) - ord(S[j])) == 32:
				k += 1
				j += 1
			else:
				li.append(str(k) + S[i].lower())
				i = j
				k = 0
			if j == len(S):
				li.append(str(k) + S[i].lower())
		return ''.join(li)


class Solution:

	def transform(self, S):
		st = ''
		c = 0
		x = S.lower()
		t = x[0]
		for i in range(len(S)):
			if x[i] == t:
				c += 1
			else:
				st += str(c)
				st += t
				c = 1
				t = x[i]
		return st + str(c) + t


class Solution:

	def transform(self, S):
		c = 1
		l = ''
		if len(S) == 1:
			l += '1'
			l += S[0].lower()
			return l
		for i in range(len(S) - 1):
			if S[i].lower() == S[i + 1].lower():
				c += 1
				if i == len(S) - 2:
					l += str(c)
					l += S[i].lower()
			else:
				l += str(c)
				l += S[i].lower()
				c = 1
		if S[-1].lower() != S[-2].lower():
			l += '1'
			l += S[-1].lower()
		return l


class Solution:

	def transform(self, S):
		S = S.lower()
		count = 1
		i = 0
		ans = ''
		if len(S) == 1:
			return '1' + S.lower()
		while i != len(S) - 2:
			if S[i] == S[i + 1]:
				count += 1
			else:
				ans += str(count) + S[i].lower()
				count = 1
			i += 1
		i += 1
		if S[i] == S[i - 1]:
			count += 1
			ans += str(count) + S[i].lower()
		else:
			ans += str(count) + S[i - 1].lower()
			count = 1
			ans += str(count) + S[i].lower()
		return ans


class Solution:

	def transform(self, S):
		S = S.lower()
		cnt = 1
		l = []
		for i in range(len(S) - 1):
			if S[i] == S[i + 1]:
				cnt += 1
			else:
				l.append(str(cnt))
				l.append(S[i])
				cnt = 1
		l.append(str(cnt))
		l.append(S[-1])
		return ''.join(l)


class Solution:

	def transform(self, S):
		c = 1
		S = S.lower()
		l = ''
		item = S[0]
		for i in S[1:]:
			if i == item:
				c += 1
			else:
				l += str(c) + item
				c = 1
				item = i
		l += str(c) + item
		return l


class Solution:

	def transform(self, S):
		if len(S) == 1:
			return '1' + S.lower()
		ans = ''
		count = 0
		for i in range(0, len(S) - 1):
			if S[i].lower() == S[i + 1].lower():
				count = count + 1
			else:
				ans = ans + str(count + 1) + S[i].lower()
				count = 0
		if S[-1].lower() == S[-2].lower():
			ans = ans + str(count + 1) + S[-1].lower()
		else:
			ans = ans + '1' + S[-1].lower()
		return ans


class Solution:

	def transform(self, S):
		s1 = S.lower()
		count = 1
		s2 = ''
		for i in range(len(s1) - 1):
			if s1[i] == s1[i + 1]:
				count += 1
			else:
				s2 += str(count)
				s2 += str(s1[i])
				count = 1
		s2 += str(count)
		s2 += str(s1[-1])
		return s2


class Solution:

	def transform(self, S):
		S = S.lower()
		res = ''
		item = S[0]
		count = 1
		for char in S[1:]:
			if char == item:
				count += 1
			else:
				res += str(count) + item
				count = 1
				item = char
		res += str(count) + item
		return res


class Solution:

	def transform(self, S):
		S = S.lower()
		result = ''
		stack = ''
		for char in S:
			if stack and char != stack[-1]:
				result += str(len(stack)) + stack[-1]
				stack = char
			else:
				stack += char
		result += str(len(stack)) + stack[-1]
		return result


class Solution:

	def transform(self, S):
		S = S.lower()
		st = S[0]
		ans = ''
		count = 1
		for i in S[1:]:
			if i in st:
				count += 1
			else:
				ans += str(count) + st
				count = 1
				st = i
		ans += str(count) + st
		return ans


class Solution:

	def transform(self, S):
		S = S.lower()
		l = len(S)
		cnt = 1
		i = 1
		r = ''
		if l == 0:
			return ''
		if l == 1:
			return '1' + S
		while i < l:
			if S[i] == S[i - 1]:
				cnt += 1
			else:
				r = r + str(cnt) + S[i - 1]
				cnt = 1
			i += 1
		r = r + str(cnt) + S[i - 1]
		return r


class Solution:

	def transform(self, S):
		S = S.lower()
		k = ''
		st = []
		for i in range(len(S)):
			if st:
				if st[-1] == S[i]:
					pass
				else:
					k += str(len(st)) + st[-1]
					st = []
			st.append(S[i])
		k += str(len(st)) + st[-1]
		return k


class Solution:

	def transform(self, S):
		ans = ''
		S = S.lower() + ' '
		s = []
		for i in S:
			if len(s) == 0:
				s.append(i)
			elif s[-1] == i:
				s.append(i)
			else:
				ans += str(len(s))
				ans += s[0]
				s = [i]
		return ans


class Solution:

	def getSmall(self, char):
		return char.lower()

	def transform(self, S):
		stack = []
		for char in S:
			char = self.getSmall(char)
			if not stack:
				stack.append([1, char])
			elif stack[-1][1] == char:
				stack[-1][0] += 1
			else:
				stack.append([1, char])
		res = ''
		for (freq, val) in stack:
			res += str(freq)
			res += val
		return res


class Solution:

	def transform(self, S):
		new = ''
		count = 1
		l = 1
		r = len(S)
		while l < r:
			if S[l].lower() == S[l - 1].lower():
				count += 1
				l += 1
			else:
				new += str(count) + S[l - 1].lower()
				count = 1
				l += 1
		new += str(count) + S[l - 1].lower()
		return new


class Solution:

	def transform(self, S):
		S = S.lower()
		S = S + ' '
		x = S[0]
		y = 0
		z = ''
		for (index, item) in enumerate(S):
			if item != x:
				z = z + str(index - y) + x
				y = index
			x = item
		return z


class Solution:

	def transform(self, s):
		s = s.lower()
		st = []
		count = 1
		for i in range(len(s) - 1):
			if s[i] != s[i + 1]:
				st.append(str(count))
				st.append(s[i])
				count = 1
			else:
				count += 1
		st.append(str(count))
		st.append(s[-1])
		return ''.join(st)


class Solution:

	def transform(self, S):
		S = S.lower()
		result = ''
		n = 1
		while len(S) > 1:
			if S[0] == S[1]:
				n += 1
				S = S[1:]
			else:
				result += str(n) + S[0]
				S = S[1:]
				n = 1
		result += str(n) + S[0]
		return result


class Solution:

	def transform(self, S):
		S = S.lower()
		res = ''
		c = 0
		st = []
		for x in S:
			if not st:
				st.append(x)
				c += 1
			elif st and x == st[-1]:
				st.append(x)
				c += 1
			elif st and x != st[-1]:
				res += str(c) + st[-1]
				st.append(x)
				c = 1
		res += str(c) + st[-1]
		return res


class Solution:

	def transform(self, S):
		S = S.lower()
		(i, j) = (0, 1)
		ans = ''
		count = 1
		if len(S) == 1:
			ans += str(count) + S[0]
			return ans
		while i < len(S) or j < len(S):
			if S[i] == S[j]:
				count += 1
				if j == len(S) - 1:
					ans += str(count) + S[i]
					break
				j += 1
			else:
				ans += str(count) + S[i]
				count = 1
				i = j
				if j == len(S) - 1:
					ans += str(count) + S[j]
					break
				j += 1
		return ans


class Solution:

	def transform(self, S):
		result = ''
		current_char = ''
		count = 0
		for ch in S:
			ch = ch.lower()
			if ch == current_char:
				count += 1
			else:
				if current_char:
					result += str(count) + current_char
				current_char = ch
				count = 1
		if current_char:
			result += str(count) + current_char
		return result


class Solution:

	def transform(self, s):
		s = s.lower()
		from itertools import groupby
		r = ''
		k = [[len(list(j)), i] for (i, j) in groupby(s)]
		for i in k:
			for j in i:
				r += str(j)
		return r


class Solution:

	def transform(self, S):
		l = list(S)
		c = 0
		l1 = []
		j = 0
		d = {}
		for i in range(len(l)):
			if l1 == []:
				l1.append(l[i].lower())
				c += 1
			elif l1[-1] == l[i].lower():
				c += 1
			else:
				d[j] = [str(c), l1.pop()]
				c = 1
				l1.append(l[i].lower())
				j += 1
		if c > 0:
			d[j] = [str(c), l1.pop()]
		d1 = sorted(d.keys())
		s = ''
		for i in d1:
			for j in d[i]:
				s += j
		return s


class Solution:

	def transform(self, s):
		s = s.lower()
		n = len(s)
		new_str = ''
		j = 0
		count = 0
		for i in range(n):
			if s[i] == s[j]:
				count += 1
			else:
				new_str = new_str + str(count) + s[j]
				j = i
				count = 1
		new_str = new_str + str(count) + s[j]
		return new_str


class Solution:

	def transform(self, S):
		numb = 1
		stack = []
		res = ''
		for i in S.lower():
			if len(stack) == 0:
				stack.append(i)
			elif i == stack[-1]:
				numb += 1
			else:
				res += str(numb) + stack[-1]
				stack.append(i)
				numb = 1
		res += str(numb) + stack[-1]
		return res


import queue

class Solution:

	def transform(self, S):
		q = queue.LifoQueue()
		for i in range(len(S) - 1, -1, -1):
			if q.qsize() and q.queue[-1][0] == S[i].lower():
				q.queue[-1][1] += 1
			else:
				q.put([S[i].lower(), 1])
		res = ''
		while q.qsize():
			k = q.get()
			res += str(k[1]) + str(k[0])
		return res


class Solution:

	def transform(self, S):
		S = S.lower()
		init = S[0]
		cc = 1
		ans = ''
		for i in S[1:]:
			if i == init:
				cc += 1
			else:
				ans += str(cc) + init
				init = i
				cc = 1
		ans += str(cc) + init
		return ans


class Solution:

	def transform(self, s):
		s = s.lower()
		if len(s) == 1:
			return '1' + s
		new_s = ''
		count = 1
		for i in range(0, len(s) - 1):
			if s[i] == s[i + 1]:
				count += 1
			else:
				new_s += str(count) + s[i]
				count = 1
		else:
			new_s += str(count) + s[i + 1]
		return new_s


class Solution:

	def transform(self, S):
		stack = []
		top = -1
		ans = ''
		for i in range(0, len(S)):
			if len(stack) == 0 or stack[top] == S[i].lower() or stack[top] == S[i].upper():
				stack.append(S[i])
			else:
				ans = ans + str(len(stack)) + stack[top].lower()
				while len(stack) > 0:
					stack.pop()
				stack.append(S[i])
		ans = ans + str(len(stack)) + stack[top].lower()
		return ans


class Solution:

	def transform(self, S):
		s = S.lower() + ' '
		c = 1
		j = 1
		r = ''
		while j < len(s):
			if s[j] == s[j - 1]:
				c += 1
			else:
				r = r + str(c) + s[j - 1]
				c = 1
			j += 1
		return r


class Solution:

	def transform(self, s):
		s = s.lower()
		s += '@!'
		l = []
		c = 1
		for i in range(len(s) - 2):
			if s[i] == s[i + 1]:
				c += 1
			else:
				l.append(c)
				l.append(s[i])
				c = 1
		f = ''
		for i in range(len(l)):
			l[i] = str(l[i])
		for i in l:
			f += i
		return f


class Solution:

	def transform(self, s):
		stack = []
		for i in range(len(s)):
			if stack:
				if stack[-1][0] == s[i].lower():
					stack[-1][-1] += 1
					continue
			stack.append([s[i].lower(), 1])
		res = ''
		for i in range(len(stack)):
			res += str(stack[i][-1]) + stack[i][0]
		return res


class Solution:

	def transform(self, S):
		S = S.lower()
		a = S[0]
		c = 1
		b = ''
		for i in S[1:]:
			if i == a:
				c += 1
			else:
				b += str(c) + a
				a = i
				c = 1
		b += str(c) + a
		return b


class Solution:

	def transform(self, S):
		z = S.lower()
		l = []
		m = ''
		n = len(z)
		for i in z:
			if len(l) == 0:
				l.append(i)
			elif l[-1] == i:
				l.append(i)
			else:
				m += str(len(l)) + l[-1]
				l = []
				l.append(i)
		m += str(len(l)) + l[-1]
		return m


class Solution:

	def transform(self, arr):
		arr = arr.lower()
		n = 0
		arr = arr + '.'
		e = arr[0]
		S = ''
		for i in arr:
			if i == e:
				n += 1
			else:
				S = S + str(n) + e
				e = i
				n = 1
		return S


class Solution:

	def transform(self, S):
		a = 1
		s = ''
		for i in range(1, len(S)):
			if S[i].lower() == S[i - 1].lower():
				a += 1
			else:
				s += str(a) + S[i - 1].lower()
				a = 1
		s += str(a) + S[-1].lower()
		return s


class Solution:

	def transform(self, S):
		S = S.lower()
		from itertools import groupby
		m = [[len(list(j)), i] for (i, j) in groupby(S)]
		r = []
		for i in m:
			for j in i:
				r.append(j)
		return ''.join(map(str, r))