Given a sentence in the form of a string in uppercase, convert it into its equivalent mobile numeric keypad sequence. Please note there might be spaces in between the words in a sentence and we can print spaces by pressing 0.
 
Example 1:
Input:
S = "GFG"
Output: 43334
Explanation: For 'G' press '4' one time.
For 'F' press '3' three times.
Example 2:
Input:
S = "HEY U"
Output: 4433999088
Explanation: For 'H' press '4' two times.
For 'E' press '3' two times. For 'Y' press '9' 
three times. For white space press '0' one time.
For 'U' press '8' two times.
 
Your Task:  
You dont need to read input or print anything. Complete the function printSequence() which takes a string as input parameter and returns its equivalent mobile numeric keypad sequence as a string.
 
Expected Time Complexity: O(Length of String)
Expected Auxiliary Space: O(Length of String)
 
Constraints:
1 <= Length of String <= 10^{5}
Characters of string can be empty space or capital alphabets.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:



    def printSequence(self,S):

        # code here



Solutions:
class Solution:

	def printSequence(self, S):
		h = {}
		h['A'] = '2'
		h['B'] = '2' * 2
		h['C'] = '2' * 3
		h['D'] = '3'
		h['E'] = '33'
		h['F'] = '333'
		h['G'] = '4'
		h['H'] = '44'
		h['I'] = '444'
		h['J'] = '5'
		h['K'] = '55'
		h['L'] = '555'
		h['M'] = '6'
		h['N'] = '66'
		h['O'] = '666'
		h['P'] = '7'
		h['Q'] = '77'
		h['R'] = '777'
		h['S'] = '7777'
		h['T'] = '8'
		h['U'] = '88'
		h['V'] = '888'
		h['W'] = '9'
		h['X'] = '99'
		h['Y'] = '999'
		h['Z'] = '9999'
		h[' '] = '0'
		s = ''
		for i in S:
			s += h[i]
		return s


class Solution:

	def printSequence(self, S):
		keypad = {2: 'abc', 3: 'def', 4: 'ghi', 5: 'jkl', 6: 'mno', 7: 'pqrs', 8: 'tuv', 9: 'wxyz', 0: ' '}
		res = ''
		for i in S.lower():
			for (x, y) in keypad.items():
				for j in y:
					if j == i:
						d = y.index(j)
						for z in range(d + 1):
							res = res + str(x)
		return res


class Solution:

	def printSequence(self, S):
		arr = ['2', '22', '222', '3', '33', '333', '4', '44', '444', '5', '55', '555', '6', '66', '666', '7', '77', '777', '7777', '8', '88', '888', '9', '99', '999', '9999']
		ans = ''
		for i in range(len(S)):
			if S[i] == ' ':
				ans = ans + '0'
			else:
				index = ord(S[i]) - ord('A')
				ans = ans + arr[index]
		return ans


class Solution:

	def printSequence(self, S):
		n_to_c_map = {2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 4: ['G', 'H', 'I'], 5: ['J', 'K', 'L'], 6: ['M', 'N', 'O'], 7: ['P', 'Q', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'X', 'Y', 'Z']}
		c_to_n_map = {'A': [2, 1], 'B': [2, 2], 'C': [2, 3], 'D': [3, 1], 'E': [3, 2], 'F': [3, 3], 'G': [4, 1], 'H': [4, 2], 'I': [4, 3], 'J': [5, 1], 'K': [5, 2], 'L': [5, 3], 'M': [6, 1], 'N': [6, 2], 'O': [6, 3], 'P': [7, 1], 'Q': [7, 2], 'R': [7, 3], 'S': [7, 4], 'T': [8, 1], 'U': [8, 2], 'V': [8, 3], 'W': [9, 1], 'X': [9, 2], 'Y': [9, 3], 'Z': [9, 4]}
		ans = ''
		for ele in S:
			if ele == ' ':
				ans += '0'
			else:
				(key, idx) = c_to_n_map[ele]
				if idx == 1:
					ans += str(key)
				elif idx == 2:
					ans += str(key)
					ans += str(key)
				elif idx == 3:
					ans += str(key)
					ans += str(key)
					ans += str(key)
				elif idx == 4:
					ans += str(key)
					ans += str(key)
					ans += str(key)
					ans += str(key)
		return ans


import math

class Solution:

	def printSequence(self, S):
		s = ''
		for x in S:
			a = ord(x) - 64
			if a < 19 and a > 0:
				y = a
				z = y / 3
				z += 1
				z = str(math.ceil(z))
				y = y % 3
				if y == 0:
					y = 3
				for i in range(y):
					s += z
			elif a == 19:
				s += '7777'
			elif a == 26:
				s += '9999'
			elif 64 - ord(x) == 32:
				s += '0'
			elif a > 19 and a < 26:
				y = a - 1
				z = y / 3
				z += 1
				z = str(math.ceil(z))
				y = y % 3
				if y == 0:
					y = 3
				for i in range(y):
					s += z
		return s


class Solution:

	def printSequence(self, S):
		list = []
		map = {'A': '2', 'B': '22', 'C': '222', 'D': '3', 'E': '33', 'F': '333', 'G': '4', 'H': '44', 'I': '444', 'J': '5', 'K': '55', 'L': '555', 'M': '6', 'N': '66', 'O': '666', 'P': '7', 'Q': '77', 'R': '777', 'S': '7777', 'T': '8', 'U': '88', 'V': '888', 'W': '9', 'X': '99', 'Y': '999', 'Z': '9999', ' ': '0'}
		for i in S:
			for (j, k) in map.items():
				if i == j:
					list.append(k)
		return ''.join(list)


class Solution:

	def printSequence(self, S):
		d = {'A': [2, 1], 'B': [2, 2], 'C': [2, 3], 'D': [3, 1], 'E': [3, 2], 'F': [3, 3], 'G': [4, 1], 'H': [4, 2], 'I': [4, 3], 'J': [5, 1], 'K': [5, 2], 'L': [5, 3], 'M': [6, 1], 'N': [6, 2], 'O': [6, 3], 'P': [7, 1], 'Q': [7, 2], 'R': [7, 3], 'S': [7, 4], 'T': [8, 1], 'U': [8, 2], 'V': [8, 3], 'W': [9, 1], 'X': [9, 2], 'Y': [9, 3], 'Z': [9, 4], ' ': [0, 1]}
		s = ''
		for i in S:
			n = d[i]
			s += str(n[0]) * n[1]
		return s


class Solution:

	def printSequence(self, s):
		k = {2: 'ABC', 3: 'DEF', 4: 'GHI', 5: 'JKL', 6: 'MNO', 7: 'PQRS', 8: 'TUV', 9: 'WXYZ', 0: ' '}
		ans = ''
		for i in s.upper():
			for (key, value) in k.items():
				for j in value:
					if i == j:
						ans += '' + str(key) * (value.index(j) + 1)
		return ans


class Solution:

	def printSequence(self, S):
		Str = ['2', '22', '222', '3', '33', '333', '4', '44', '444', '5', '55', '555', '6', '66', '666', '7', '77', '777', '7777', '8', '88', '888', '9', '99', '999', '9999']
		output = ''
		for i in S:
			if i == ' ':
				output += '0'
			else:
				asc = ord(i)
				n = int(asc - 65)
				output += Str[n]
		return output


class Solution:

	def printSequence(self, S):
		n = [0, 2, 22, 222, 3, 33, 333, 4, 44, 444, 5, 55, 555, 6, 66, 666, 7, 77, 777, 7777, 8, 88, 888, 9, 99, 999, 9999]
		a = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
		num = ''
		for i in range(len(S)):
			num = num + str(n[a.index(S[i])])
		return num


class Solution:

	def printSequence(self, S):
		temp = {'a': '2', 'b': '22', 'c': '222', 'd': '3', 'e': '33', 'f': '333', 'g': '4', 'h': '44', 'i': '444', 'j': '5', 'k': '55', 'l': '555', 'm': '6', 'n': '66', 'o': '666', 'p': '7', 'q': '77', 'r': '777', 's': '7777', 't': '8', 'u': '88', 'v': '888', 'w': '9', 'x': '99', 'y': '999', 'z': '9999', ' ': '0'}
		res = ''
		for ch in S.lower():
			res += temp[ch]
		return res


class Solution:

	def printSequence(self, S):
		all_ltr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		dict_map = {}
		cnt = 2
		i = 0
		j = 0
		while i < len(all_ltr):
			dict_map[all_ltr[i]] = str(cnt) + str(j + 1)
			lim = 4 if i in range(15, 19) or i in range(22, 26) else 3
			if j < lim:
				j += 1
			if j == lim:
				cnt += 1
				j = 0
			i += 1
		dict_map[' '] = '01'
		ans = ''
		for i in S:
			ans += dict_map[i][0] * int(dict_map[i][1])
		return ans


class Solution:

	def printSequence(self, S):
		a = []
		c = ''
		for i in S:
			a.append(i)
			if i == '1':
				c += '1'
			elif i == 'A':
				c += '2'
			elif i == 'B':
				c += '22'
			elif i == 'C':
				c += '222'
			elif i == 'D':
				c += '3'
			elif i == 'E':
				c += '33'
			elif i == 'F':
				c += '333'
			elif i == 'G':
				c += '4'
			elif i == 'H':
				c += '44'
			elif i == 'I':
				c += '444'
			elif i == 'J':
				c += '5'
			elif i == 'K':
				c += '55'
			elif i == 'L':
				c += '555'
			elif i == 'M':
				c += '6'
			elif i == 'N':
				c += '66'
			elif i == 'O':
				c += '666'
			elif i == 'P':
				c += '7'
			elif i == 'Q':
				c += '77'
			elif i == 'R':
				c += '777'
			elif i == 'S':
				c += '7777'
			elif i == 'T':
				c += '8'
			elif i == 'U':
				c += '88'
			elif i == 'V':
				c += '888'
			elif i == 'W':
				c += '9'
			elif i == 'X':
				c += '99'
			elif i == 'Y':
				c += '999'
			elif i == 'Z':
				c += '9999'
			elif i == ' ':
				c += '0'
			else:
				print('invalid')
		return c


class Solution:

	def printSequence(self, S):
		a = [2, 22, 222, 3, 33, 333, 4, 44, 444, 5, 55, 555, 6, 66, 666, 7, 77, 777, 7777, 8, 88, 888, 9, 99, 999, 9999]
		res = ''
		for each in S:
			if each == ' ':
				res += '0'
			else:
				x = ord(each)
				num = x - 65
				res += str(a[num])
		return res


class Solution:

	def printSequence(self, S):
		k = []
		l = [' ', 'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ']
		x = ''
		for a in S:
			for i in range(len(l)):
				for j in range(len(l[i])):
					if l[i][j] == a:
						if a == ' ':
							x += '0'
						else:
							x += str(i + 1) * (j + 1)
		return x


class Solution:

	def printSequence(self, S):
		number = ''
		for i in S:
			a = ord(i) - ord('A')
			a += 1
			if i == 'S':
				number += '7' * 4
				continue
			elif i == 'Z':
				number += '9' * 4
				continue
			elif i == ' ':
				number += '0'
			else:
				if a >= 20:
					a -= 1
				a += 2
				reps = a % 3 + 1
				div = str(a // 3 + 1)
				number += div * reps
		return number


class Solution:

	def printSequence(self, S):
		str1 = ['2', '22', '222', '3', '33', '333', '4', '44', '444', '5', '55', '555', '6', '66', '666', '7', '77', '777', '7777', '8', '88', '888', '9', '99', '999', '9999']
		st1 = ''
		for i in S:
			if i == ' ':
				st1 += '0'
				continue
			else:
				v = ord(i) - 65
				st1 += str1[v]
		return st1


class Solution:

	def printSequence(self, S):
		two = ['A', 'B', 'C']
		three = ['D', 'E', 'F']
		four = ['G', 'H', 'I']
		five = ['J', 'K', 'L']
		six = ['M', 'N', 'O']
		seven = ['P', 'Q', 'R', 'S']
		eight = ['T', 'U', 'V']
		nine = ['W', 'X', 'Y', 'Z']
		zero = ' '
		res = []
		for i in S:
			if i == zero:
				res.append(0)
			if i in two:
				for j in range(len(two)):
					if two[j] == i:
						res.append(2)
						break
					else:
						res.append(2)
			if i in three:
				for j in range(len(three)):
					if three[j] == i:
						res.append(3)
						break
					else:
						res.append(3)
			if i in four:
				for j in range(len(four)):
					if four[j] == i:
						res.append(4)
						break
					else:
						res.append(4)
			if i in five:
				for j in range(len(five)):
					if five[j] == i:
						res.append(5)
						break
					else:
						res.append(5)
			if i in six:
				for j in range(len(six)):
					if six[j] == i:
						res.append(6)
						break
					else:
						res.append(6)
			if i in seven:
				for j in range(len(seven)):
					if seven[j] == i:
						res.append(7)
						break
					else:
						res.append(7)
			if i in eight:
				for j in range(len(eight)):
					if eight[j] == i:
						res.append(8)
						break
					else:
						res.append(8)
			if i in nine:
				for j in range(len(nine)):
					if nine[j] == i:
						res.append(9)
						break
					else:
						res.append(9)
		string = ''
		for num in res:
			string += str(num)
		return string


class Solution:

	def printSequence(self, s):
		p = ''
		dic = {2: 'ABC', 3: 'DEF', 4: 'GHI', 5: 'JKL', 6: 'MNO', 7: 'PQRS', 8: 'TUV', 9: 'WXYZ'}
		for i in s:
			if i == ' ':
				p += '0'
				continue
			for (j, k) in dic.items():
				if i in k:
					p += (k.index(i) + 1) * str(j)
		return p


class Solution:

	def printSequence(self, S):
		str = ['2', '22', '222', '3', '33', '333', '4', '44', '444', '5', '55', '555', '6', '66', '666', '7', '77', '777', '7777', '8', '88', '888', '9', '99', '999', '9999']
		n = len(S)
		output = ''
		for i in S:
			if i == ' ':
				output = output + '0'
			else:
				position = ord(i) - ord('A')
				output = output + str[position]
		return output


class Solution:

	def printSequence(self, S):
		res = ''
		for i in S:
			alpha = ord(i) - 62
			if alpha >= 0:
				if alpha not in [21, 28]:
					if alpha in range(22, 28):
						alpha -= 1
					key = alpha // 3 + 1
					for _ in range(alpha % 3 + 1):
						res += str(key)
				elif alpha == 21:
					for _ in range(4):
						res += '7'
				elif alpha == 28:
					for _ in range(4):
						res += '9'
			elif alpha < 0:
				res += '0'
		return res


class Solution:

	def printSequence(self, S):
		mapping = ['2', '22', '222', '3', '33', '333', '4', '44', '444', '5', '55', '555', '6', '66', '666', '7', '77', '777', '7777', '8', '88', '888', '9', '99', '999', '9999']
		result = ''
		for char in S:
			if char == ' ':
				result += '0'
			else:
				result += mapping[ord(char) - ord('A')]
		return result


class Solution:

	def printSequence(self, S):
		c = 0
		s = 'ABC1 DEF2 GHI3 JKL4 MNO5 PQRS TUV6 WXYZ'
		x = s.split(' ', 7)
		ls = []
		for i in range(len(S)):
			if S[i] == ' ':
				ls.append(0)
			for j in range(len(x)):
				for c in range(4):
					if S[i] == x[j][c]:
						for u in range(c + 1):
							ls.append(j + 2)
		res = ''.join((str(e) for e in ls))
		return res


class Solution:

	def printSequence(self, S):
		finalans = ''
		cl = [' ', 'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ']
		nl = [0, 2, 3, 4, 5, 6, 7, 8, 9]
		for i in S:
			for j in cl:
				if i in j:
					string = j
					break
			index = cl.index(string)
			tempans = nl[index]
			for j in string:
				if i == j:
					finalans += str(tempans)
					break
				finalans += str(tempans)
		return finalans


class Solution:

	def printSequence(self, S):
		dict_ = {'1': ['1'], '2': ['A', 'B', 'C'], '3': ['D', 'E', 'F'], '4': ['G', 'H', 'I'], '5': ['J', 'K', 'L'], '6': ['M', 'N', 'O'], '7': ['P', 'Q', 'R', 'S'], '8': ['T', 'U', 'V'], '9': ['W', 'X', 'Y', 'Z'], '0': [' ']}
		num_equivalent = []
		for letter in S:
			for number in dict_:
				if letter in dict_[number]:
					index = dict_[number].index(letter)
					num_equivalent.extend([number] * (index + 1))
		return ''.join(num_equivalent)


class Solution:

	def printSequence(self, S):
		a = []
		for i in range(len(S)):
			if S[i] == 'A':
				a.append('2')
			elif S[i] == 'B':
				a.append('2' * 2)
			elif S[i] == 'C':
				a.append('2' * 3)
			elif S[i] == 'D':
				a.append('3')
			elif S[i] == 'E':
				a.append('3' * 2)
			elif S[i] == 'F':
				a.append('3' * 3)
			elif S[i] == 'G':
				a.append('4' * 1)
			elif S[i] == 'H':
				a.append('4' * 2)
			elif S[i] == 'I':
				a.append('4' * 3)
			elif S[i] == 'J':
				a.append('5' * 1)
			elif S[i] == 'K':
				a.append('5' * 2)
			elif S[i] == 'L':
				a.append('5' * 3)
			elif S[i] == 'M':
				a.append('6' * 1)
			elif S[i] == 'N':
				a.append('6' * 2)
			elif S[i] == 'O':
				a.append('6' * 3)
			elif S[i] == 'P':
				a.append('7' * 1)
			elif S[i] == 'Q':
				a.append('7' * 2)
			elif S[i] == 'R':
				a.append('7' * 3)
			elif S[i] == 'S':
				a.append('7' * 4)
			elif S[i] == 'T':
				a.append('8' * 1)
			elif S[i] == 'U':
				a.append('8' * 2)
			elif S[i] == 'V':
				a.append('8' * 3)
			elif S[i] == 'W':
				a.append('9' * 1)
			elif S[i] == 'X':
				a.append('9' * 2)
			elif S[i] == 'Y':
				a.append('9' * 3)
			elif S[i] == 'Z':
				a.append('9' * 4)
			elif S[i] == ' ':
				a.append('0')
		return ''.join(a)


class Solution:

	def printSequence(self, S):
		output = ''
		dict = {'ABC': 2, 'DEF': 3, 'GHI': 4, 'JKL': 5, 'MNO': 6, 'PQRS': 7, 'TUV': 8, 'WXYZ': 9}
		for c in S:
			for (key, value) in enumerate(dict):
				if c in value:
					for i in range(value.index(c) + 1):
						output = output + str(dict.get(value, key))
				if c is ' ':
					output = output + '0'
					break
		return output