Given a string, Your task is to  complete the function encode that returns the run length encoded string for the given string.
eg if the input string is “wwwwaaadexxxxxx”, then the function should return “w4a3d1e1x6″.
You are required to complete the function encode that takes only one argument the string which is to be encoded and returns the encoded string.
Example 1:
Input:
str = aaaabbbccc
Output: a4b3c3
Explanation: a repeated 4 times
consecutively b 3 times, c also 3
times.
Example 2:
Input:
str = abbbcdddd
Output: a1b3c1d4
Your Task:
Complete the function encode() which takes a character array as a input parameter and returns the encoded string.
Expected Time Complexity: O(N), N = length of given string.
Expected Auxiliary Space: O(1)
Constraints:
1<=length of str<=100

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #Your task is to complete this function
#Function should return complete string
def encode(arr):
    # Code here

Solutions:
def encode(arr):
	(temp, count, ans) = (arr[0], 1, '')
	for i in range(1, len(arr)):
		if temp != arr[i]:
			ans += temp + str(count)
			temp = arr[i]
			count = 1
		else:
			count += 1
	return ans + temp + str(count)


def encode(string):
	os = ''
	for i in range(0, len(string)):
		char = string[i]
		counter = 0
		if i > 0:
			if string[i] == string[i - 1]:
				continue
		for j in range(i, len(string)):
			if char == string[j]:
				counter = counter + 1
			else:
				break
		os += char + str(counter)
	return os


def encode(arr):
	s = arr
	fi = ''
	count = 1
	if len(s) == 1:
		fi += s[0] + str(1)
		return fi
	for i in range(len(s) - 1):
		if i == len(s) - 2:
			if s[i] != s[i + 1]:
				fi += s[i] + str(count) + s[i + 1] + str(1)
			else:
				count += 1
				fi += s[i] + str(count)
		elif s[i] != s[i + 1]:
			fi += s[i] + str(count)
			count = 1
		else:
			count += 1
	return fi


def encode(arr):
	k = arr[0]
	n = 1
	ans = ''
	for i in range(1, len(arr)):
		if arr[i] != k:
			ans += k + str(n)
			k = arr[i]
			n = 1
		else:
			n += 1
	ans += k + str(n)
	return ans


def encode(arr):
	output_str = ''
	consecutive_count = 1
	for i in range(0, len(arr) - 1):
		if arr[i] == arr[i + 1]:
			consecutive_count += 1
		elif i == len(arr) - 1 and arr[i] == arr[i - 1]:
			output_str += arr[i]
			output_str += str(consecutive_count)
		else:
			output_str += arr[i]
			output_str += str(consecutive_count)
			consecutive_count = 1
	output_str += arr[-1] + str(consecutive_count)
	return output_str


def encode(arr):
	last = arr[0]
	res = arr[0]
	k = 1
	for s in arr[1:]:
		if last == s:
			k += 1
		else:
			res += str(k)
			k = 1
			last = s
			res += s
	res += str(k)
	return res


def encode(arr):
	l = []
	result = ''
	count = 0
	if len(arr) > 1:
		for i in range(len(arr) - 1):
			if arr[i] == arr[i + 1]:
				count += 1
			else:
				l.append([arr[i], count])
				count = 0
		if arr[-1] == arr[-2]:
			l.append([arr[-1], count])
		else:
			l.append([arr[-1], 0])
		for i in range(len(l)):
			result += str(l[i][0]) + str(l[i][1] + 1)
	else:
		return str(arr[0]) + str(1)
	return result


def encode(arr):
	cnt = 0
	tmp = []
	st = ''
	for i in arr:
		if tmp and i == tmp[-1]:
			cnt += 1
		else:
			if tmp:
				st += tmp[0] + str(cnt + 1)
				cnt = 0
				tmp = []
			tmp.append(i)
	if tmp:
		st += tmp[0] + str(cnt + 1)
	return st


def encode(arr):
	ret = ''
	n = len(arr)
	i = 0
	while i < n:
		c = arr[i]
		count = 1
		j = i + 1
		while j < n and arr[j] == c:
			(j, count) = (j + 1, count + 1)
		ret += c + str(count)
		i = j
	return ret


def encode(arr):
	s = ''
	count = 0
	for i in range(len(arr) - 1):
		count += 1
		if arr[i] != arr[i + 1]:
			s += arr[i]
			s += str(count)
			count = 0
	s += arr[-1]
	s += str(count + 1)
	return s


def encode(arr):
	s = ''
	c = 1
	if len(arr) == 1:
		s = arr[0] + '1'
		return s
	for i in range(len(arr) - 1):
		if arr[i] == arr[i + 1]:
			c += 1
		else:
			s += arr[i] + str(c)
			c = 1
	s += arr[i + 1] + str(c)
	return s


from collections import Counter

def encode(arr):
	(temp, count, ans) = (arr[0], 1, '')
	for i in range(1, len(arr)):
		if temp != arr[i]:
			ans += temp + str(count)
			temp = arr[i]
			count = 1
		else:
			count += 1
	return ans + temp + str(count)


def encode(arr):
	count = 1
	arr1 = []
	arr2 = arr + '*'
	for i in range(len(arr)):
		if arr2[i] == arr2[i + 1]:
			count += 1
		else:
			arr1.append(arr2[i])
			arr1.append(count)
			count = 1
	return ''.join(map(str, arr1))


def encode(arr):
	p = ''
	c = 1
	if len(arr) == 1:
		p = arr[0] + '1'
		return p
	for i in range(len(arr) - 1):
		x = arr[i]
		if arr[i] == arr[i + 1]:
			c += 1
		else:
			p = p + x + str(c)
			c = 1
	if arr[-1] == arr[-2]:
		p = p + x + str(c)
	else:
		p = p + arr[-1] + '1'
	return p


def encode(arr):
	i = 0
	res = ''
	while i < len(arr):
		j = i
		while j < len(arr) and arr[j] == arr[i]:
			j += 1
		res += arr[i] + str(j - i)
		i = j
	return res


def encode(arr):
	n = len(arr)
	if n == 1:
		return arr[0] + '1'
	else:
		count = 0
		res = ''
		for i in range(n - 1):
			if arr[i] == arr[i + 1]:
				count += 1
			else:
				res += arr[i] + str(count + 1)
				count = 0
		res += arr[n - 1] + str(count + 1)
		return res


from collections import OrderedDict

def encode(arr):
	arr = arr + '$'
	out = ''
	first = arr[0]
	count = 0
	for i in range(len(arr)):
		current = arr[i]
		if arr[i] == first:
			count += 1
		else:
			out += first + str(count)
			first = current
			count = 1
	return out


def encode(arr):
	ans = ''
	prevchar = ''
	count = 0
	for i in arr:
		if prevchar == '':
			prevchar = i
			count += 1
		elif prevchar == i:
			count += 1
		else:
			ans = ans + prevchar + str(count)
			prevchar = i
			count = 1
	ans = ans + prevchar + str(count)
	return ans


def encode(arr):
	res = str(arr[0])
	bef = arr[0]
	cou = 0
	for i in range(0, len(arr)):
		if arr[i] == bef:
			cou += 1
		elif arr[i] != bef:
			bef = str(arr[i])
			res += str(cou)
			res += str(arr[i])
			cou = 1
	res += str(cou)
	return res


def encode(arr):
	list1 = []
	w = ''
	for i in range(0, len(arr)):
		c = 1
		if i not in list1:
			list1.append(i)
			for j in range(i + 1, len(arr)):
				if arr[i] == arr[j]:
					c = c + 1
					list1.append(j)
				else:
					break
			w = w + arr[i] + str(c)
	return w


from collections import defaultdict

def encode(arr):
	res = ''
	dt = ''
	for i in range(len(arr)):
		if dt == '':
			dt += arr[i]
		elif arr[i] != dt[-1]:
			res += dt[0] + str(len(dt))
			dt = arr[i]
		else:
			dt += arr[i]
	if len(dt) != 0:
		res += dt[0] + str(len(dt))
	return res


def encode(arr):
	x = []
	count = 1
	for i in range(len(arr) - 1):
		if arr[i] == arr[i + 1]:
			count += 1
		else:
			x += [arr[i] + str(count)]
			count = 1
	x += [arr[-1] + str(count)]
	return ''.join(x)


def encode(arr):
	count = 1
	new_str = ''
	pre_ch = arr[0]
	for i in arr[1:]:
		if i == pre_ch:
			count += 1
		else:
			new_str += pre_ch + str(count)
			count = 1
			pre_ch = i
	new_str += pre_ch + str(count)
	return new_str


def encode(s):
	c = 1
	string = ''
	for i in range(len(s) - 1):
		if s[i] == s[i + 1]:
			c += 1
		else:
			string += s[i] + str(c)
			c = 1
	if c != 1:
		string += s[-1] + str(c)
	else:
		string += s[-1] + str(c)
	return string


def encode(arr):
	ans = ''
	(l, r) = (0, 0)
	while r < len(arr):
		if arr[l] != arr[r]:
			ans += arr[l]
			ans += str(r - l)
			l = r
		if r == len(arr) - 1:
			ans += arr[l]
			ans += str(r - l + 1)
		r += 1
	return ans


def encode(arr):
	i = 0
	cnt = 0
	res = []
	while i < len(arr):
		cnt = 1
		while i + 1 < len(arr) and arr[i] == arr[i + 1]:
			cnt += 1
			i += 1
		i += 1
		res.append(arr[i - 1])
		res.append(cnt)
	return ''.join([str(ele) for ele in res])


def encode(arr):
	stack = []
	for ele in arr:
		if not stack:
			stack.append(ele)
			stack.append(1)
		else:
			cnt = stack.pop()
			if stack[-1] == ele:
				stack.append(cnt + 1)
			else:
				stack.append(cnt)
				stack.append(ele)
				stack.append(1)
	return ''.join([str(ele) for ele in stack])


def encode(arr):
	n = len(arr)
	res = ''
	t = 1
	for i in range(n - 1):
		if arr[i] == arr[i + 1]:
			t += 1
		else:
			res += arr[i] + str(t)
			t = 1
	res += arr[n - 1] + str(t)
	return res


def encode(arr):
	temp = arr[0]
	s1 = ''
	c = 1
	for i in range(1, len(arr)):
		if temp != arr[i]:
			s1 += temp + str(c)
			c = 1
			temp = arr[i]
		else:
			c += 1
	return s1 + temp + str(c)


def encode(arr):
	results = ''
	count = 1
	current_char = arr[0]
	for i in range(1, len(arr)):
		if arr[i] == current_char:
			count += 1
		else:
			results += current_char + str(count)
			count = 1
			current_char = arr[i]
	results += current_char + str(count)
	return results


def encode(arr):
	ans = ''
	count = 0
	for i in range(len(arr)):
		if i == len(arr) - 1:
			if arr[i] == arr[i - 1]:
				ans += arr[i]
				ans += str(len(arr[:i + 1]) - count)
			else:
				ans += arr[i - 1]
				ans += str(len(arr[:i]) - count)
				count += len(arr[:i]) - count
				ans += arr[i] + '1'
		elif i == 0 or arr[i - 1] == arr[i]:
			continue
		else:
			ans += arr[i - 1]
			ans += str(len(arr[:i]) - count)
			count += len(arr[:i]) - count
	return ans[0:]


def encode(arr):
	p1 = p2 = 0
	result = ''
	if arr == '':
		return None
	while p2 <= len(arr) - 1:
		if arr[p1] != arr[p2]:
			result += arr[p1]
			result += str(p2 - p1)
			p1 = p2
		p2 += 1
	result += arr[p1]
	result += str(p2 - p1)
	return result


def encode(arr):
	l = ''
	x = arr[0]
	c = 1
	for i in range(1, len(arr)):
		y = arr[i]
		if x == y:
			c += 1
		else:
			l += x
			l += str(c)
			x = y
			c = 1
	else:
		l += x
		l += str(c)
	return l


def encode(arr):
	r = ''
	count = 1
	for i in range(1, len(arr)):
		if arr[i] == arr[i - 1]:
			count += 1
		else:
			r += arr[i - 1] + str(count)
			count = 1
	r += arr[-1] + str(count)
	return r


def encode(arr):
	res = ''
	i = 0
	while i < len(arr):
		ch = arr[i]
		count = 1
		while i < len(arr) - 1 and arr[i + 1] == arr[i]:
			count += 1
			i += 1
		i += 1
		res += ch + str(count)
	return res


def encode(arr):
	count = 1
	encode = ''
	i = 0
	while i < len(arr) - 1:
		if arr[i] == arr[i + 1]:
			count += 1
			i += 1
		else:
			encode += arr[i] + str(count)
			count = 1
			i += 1
	encode += arr[i] + str(count)
	return encode


def encode(s):
	i = 0
	count = 1
	temp = 0
	result = ''
	while i < len(s):
		if i + 1 < len(s):
			if s[i] != s[i + 1]:
				result = result + s[i] + str(count)
				count = 1
			else:
				count += 1
		elif count == 1:
			result = result + s[i] + str(count)
		else:
			result = result + s[i] + str(count)
		i += 1
	return result


def encode(arr):
	ans = ''
	curr = arr[0]
	cnt = 1
	for i in range(1, len(arr)):
		if curr == arr[i]:
			cnt += 1
		else:
			ans += curr + str(cnt)
			curr = arr[i]
			cnt = 1
	ans += curr + str(cnt)
	return ans


def encode(arr):
	ans = ''
	i = 0
	j = 0
	while j < len(arr):
		ct = 0
		while j < len(arr) and arr[i] == arr[j]:
			j += 1
			ct += 1
		ans = ans + arr[i] + str(ct)
		i = i + ct
	return ans


def encode(str1):
	if len(str1) == 0:
		return ''
	new_list = []
	new_str = ''
	new_str_return = ''
	val = 1
	pre = ''
	str1 = str1 + '$'
	for i in range(1, len(str1)):
		if str1[i - 1] != str1[i]:
			new_str_return = new_str_return + str1[i - 1] + str(val)
			val = 0
		val = val + 1
	return new_str_return


def encode(arr):
	r = arr[0]
	s = ''
	c = 0
	i = 0
	while i < len(arr):
		j = i + 1
		s += arr[i]
		c = 1
		while j < len(arr) and arr[j] == arr[i]:
			j += 1
			c += 1
		if c != 1:
			s += str(c)
			i = j
		else:
			s += str(c)
			i += 1
	return s


def encode(string):
	encoded_string = ''
	count = 1
	for i in range(1, len(string)):
		if string[i] == string[i - 1]:
			count += 1
		else:
			encoded_string += string[i - 1] + str(count)
			count = 1
	encoded_string += string[-1] + str(count)
	return encoded_string


def encode(arr):
	previous = arr[0]
	out = ''
	c = 0
	i = 1
	if len(arr) == 1:
		out = out + previous + str(i)
	else:
		for i in range(len(arr)):
			if arr[i] == previous:
				c = c + 1
			else:
				out = out + previous + str(c)
				previous = arr[i]
				c = 1
			if i == len(arr) - 1:
				out = out + previous + str(c)
	return out


def encode(st):
	stri = ''
	n = len(st)
	i = 0
	while i < n - 1:
		count = 1
		while i < n - 1 and st[i] == st[i + 1]:
			count += 1
			i += 1
		i += 1
		stri += st[i - 1] + str(count)
	if st[n - 1] != st[n - 2] or n == 1:
		stri += st[n - 1] + '1'
	return stri


def encode(arr):
	s = arr[0]
	n = 1
	for c in arr[1:]:
		if c == s[-1]:
			n += 1
		elif c != s[-1]:
			s += str(n) + c
			n = 1
	s += str(n)
	return s


def encode(arr):
	result = ''
	i = 0
	while i < len(arr):
		count = 1
		while i < len(arr) - 1 and arr[i] == arr[i + 1]:
			count += 1
			i += 1
		result += arr[i] + str(count)
		i += 1
	return result


def encode(arr):
	ans = ''
	i = 0
	while i < len(arr):
		s = arr[i]
		count = 0
		while i < len(arr) and arr[i] == s:
			i += 1
			count += 1
		ans += s
		ans += str(count)
	return ans


def encode(arr):
	cnt = 0
	string = ''
	i = 0
	while i < len(arr):
		string += arr[i]
		cnt = 1
		while i + 1 < len(arr) and arr[i] == arr[i + 1]:
			cnt += 1
			i += 1
		if i + 1 < len(arr) and arr[i] == arr[i + 1]:
			continue
		else:
			i += 1
		string += str(cnt)
	return string


from itertools import groupby

def encode(arr):
	a = groupby(arr)
	b = [(label, str(sum((1 for _ in group)))) for (label, group) in a]
	c = ''.join([j for i in b for j in i])
	return c


def encode(arr):
	i = 1
	res = ''
	while i < len(arr):
		c = 0
		while i < len(arr) and arr[i] == arr[i - 1]:
			i += 1
			c += 1
		res += arr[i - 1] + str(c + 1)
		if i == len(arr):
			return res
		i += 1
	res += arr[len(arr) - 1] + '1'
	return res


def encode(A):
	(i, j) = (0, 0)
	N = len(A)
	ans = []
	while j < N:
		if A[j] == A[i]:
			j += 1
		else:
			ans.append(A[i])
			ans.append(str(j - i))
			i = j
	ans.append(A[i])
	ans.append(str(j - i))
	return ''.join(ans)


def encode(st):
	s = list(st)
	n = len(s)
	k = s[-1]
	s = s[:-1]
	count = 1
	p = 0
	l = []
	while p <= n - 2:
		if s[-1] == k:
			count += 1
			s.pop()
		else:
			l.append(count)
			l.append(k)
			k = s[-1]
			s.pop()
			count = 1
		p += 1
	l.append(count)
	l.append(k)
	l.reverse()
	return ''.join(map(str, l))


def encode(st):
	n = len(st)
	i = 0
	l = []
	k = 0
	while i < n - 1:
		count = 1
		while i < n - 1 and st[i] == st[i + 1]:
			count += 1
			i += 1
			k += 1
		i += 1
		k += 1
		l.append(st[i - 1])
		l.append(count)
	if k == n - 1:
		l.append(st[n - 1])
		l.append(1)
	s = ''.join(map(str, l))
	return s


def encode(arr):
	prev = arr[0]
	c = 1
	ans = ''
	for ele in arr[1:]:
		if ele != prev:
			ans += str(prev) + str(c)
			c = 0
			prev = ele
		c += 1
	ans += str(prev) + str(c)
	return ans


def encode(arr):
	str_val = list(arr)
	inv = 0
	count = 0
	temp_list = []
	for i in range(len(str_val)):
		if str_val[inv] == str_val[i]:
			count += 1
		else:
			temp_list.append(str_val[inv] + str(count))
			inv = i
			count = 1
	temp_list.append(str_val[inv] + str(count))
	finl = ''.join(temp_list)
	return finl


def encode(arr):
	n = len(arr)
	i = 0
	count = 0
	l = []
	while i <= n - 1:
		count = 1
		while i < n - 1 and arr[i] == arr[i + 1]:
			count += 1
			i += 1
		i += 1
		l.append(arr[i - 1] + str(count))
	return ''.join(l)


def encode(arr):
	count = 1
	st = ''
	i = 0
	if len(arr) == 1:
		return arr[0] + '1'
	for i in range(0, len(arr) - 1):
		if i != None:
			if arr[i] == arr[i + 1]:
				count += 1
			else:
				st = st + arr[i] + str(count)
				count = 1
	st = st + arr[i + 1] + str(count)
	return st


def encode(arr):
	c = 1
	ns = ''
	for i in range(len(arr) - 1):
		if arr[i] == arr[i + 1]:
			c += 1
		elif arr[i] != arr[i + 1]:
			ns += arr[i] + str(c)
			c = 1
	ns += arr[-1] + str(c)
	return ns


def encode(arr):
	k = ''
	u = ''
	k += arr[0]
	for i in range(1, len(arr)):
		if arr[i] in k:
			k += arr[i]
		else:
			u += k[0] + str(len(k))
			k = ''
			k += arr[i]
	u += k[0] + str(len(k))
	return u


def encode(arr):
	i = 0
	freq = 0
	new = ''
	current = arr[0]
	new += current
	for char in arr:
		if arr[i] != current:
			if freq > 0:
				new += str(freq)
			current = arr[i]
			new += arr[i]
			freq = 1
		elif arr[i] == current:
			freq += 1
		i += 1
		if i == len(arr):
			new += str(freq)
	return new


def encode(arr):
	if arr == '':
		return ''
	res = ''
	count = 1
	prevEle = arr[0]
	for i in range(1, len(arr)):
		if arr[i] == prevEle:
			count += 1
			continue
		res += prevEle
		res += str(count)
		prevEle = arr[i]
		count = 1
	res += prevEle
	res += str(count)
	return res


def encode(arr):
	symb = arr[0]
	result = ''
	count = 0
	for i in arr:
		if i != symb:
			result += symb + '' + str(count)
			count = 1
			symb = i
		else:
			symb = i
			count += 1
	result += symb + '' + str(count)
	return result