You are given a string S of lowercase alphabet characters and the task is to find its matching decimal representation as on the shown keypad. Output the decimal representation corresponding to the string. For ex: if you are given amazon then its corresponding decimal representation will be 262966.
Example 1:
Input:
S = geeksforgeeks
Output: 4335736743357
Explanation:geeksforgeeks is 4335736743357
in decimal when we type it using the given
keypad.
Example 2:
Input:
S = geeksquiz
Output: 433577849
Explanation: geeksquiz is 433577849 in
decimal when we type it using the given
keypad.
Your Task:
Complete printNumber() function that takes string s and its length as parameters and returns the corresponding decimal representation of the given string as a string type. The printing is done by the driver code.
Constraints:
1 ≤ length of String ≤ 100
Expected Time Complexity : O(n)
Expected Auxilliary Space : O(n)

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3





#Function to find matching decimal representation of a string as on the keypad.

def printNumber(s,n):

   

    #CODE HERE

    

Solutions:
def printNumber(s, n):
	s = s.upper()
	ans = ''
	my_dict = {'A': 2, 'B': 2, 'C': 2, 'D': 3, 'E': 3, 'F': 3, 'G': 4, 'H': 4, 'I': 4, 'J': 5, 'K': 5, 'L': 5, 'M': 6, 'N': 6, 'O': 6, 'P': 7, 'Q': 7, 'R': 7, 'S': 7, 'T': 8, 'U': 8, 'V': 8, 'W': 9, 'X': 9, 'Y': 9, 'Z': 9}
	for i in range(len(s)):
		ans += str(my_dict[s[i]])
	return ans


def printNumber(s, n):
	d = [2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9]
	res = ''
	for i in s:
		res += str(d[ord(i) - ord('a')])
	return res


def printNumber(s, n):
	s = s.lower()
	d = {'a': '2', 'b': '2', 'c': '2', 'd': '3', 'e': '3', 'f': '3', 'g': '4', 'h': '4', 'i': '4', 'j': '5', 'k': '5', 'l': '5', 'm': '6', 'n': '6', 'o': '6', 'p': '7', 'q': '7', 'r': '7', 's': '7', 't': '8', 'u': '8', 'v': '8', 'w': '9', 'x': '9', 'y': '9', 'z': '9'}
	result = ''
	for c in s:
		result = result + d[c]
	return result


def printNumber(s, n):
	freq = {'a': 2, 'b': 2, 'c': 2, 'd': 3, 'e': 3, 'f': 3, 'g': 4, 'h': 4, 'i': 4, 'j': 5, 'k': 5, 'l': 5, 'm': 6, 'n': 6, 'o': 6, 'p': 7, 'q': 7, 'r': 7, 's': 7, 't': 8, 'u': 8, 'v': 8, 'w': 9, 'x': 9, 'y': 9, 'z': 9}
	ans = ''
	for ele in s:
		if ele in freq:
			ans += str(freq[ele])
	return ans


def printNumber(s, n):
	(n2, n3, n4, n5, n6, n7, n8, n9) = ('abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz')
	res = ''
	for i in s:
		if i in n2:
			res += '2'
		elif i in n3:
			res += '3'
		elif i in n4:
			res += '4'
		elif i in n5:
			res += '5'
		elif i in n6:
			res += '6'
		elif i in n7:
			res += '7'
		elif i in n8:
			res += '8'
		elif i in n9:
			res += '9'
	return res


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


def printNumber(str, n):
	s = ''
	for i in range(len(str)):
		if str[i] == 'a' or str[i] == 'b' or str[i] == 'c':
			s += '2'
		elif str[i] == 'd' or str[i] == 'e' or str[i] == 'f':
			s += '3'
		elif str[i] == 'g' or str[i] == 'h' or str[i] == 'i':
			s += '4'
		elif str[i] == 'j' or str[i] == 'k' or str[i] == 'l':
			s += '5'
		elif str[i] == 'm' or str[i] == 'n' or str[i] == 'o':
			s += '6'
		elif str[i] == 'p' or str[i] == 'q' or str[i] == 'r' or (str[i] == 's'):
			s += '7'
		elif str[i] == 't' or str[i] == 'u' or str[i] == 'v':
			s += '8'
		else:
			s += '9'
	return s


def printNumber(s, n):
	l1 = []
	for i in s:
		if i in {'a', 'b', 'c'}:
			l1.append('2')
		elif i in {'d', 'e', 'f'}:
			l1.append('3')
		elif i in {'g', 'h', 'i'}:
			l1.append('4')
		elif i in {'j', 'k', 'l'}:
			l1.append('5')
		elif i in {'m', 'n', 'o'}:
			l1.append('6')
		elif i in {'p', 'q', 'r', 's'}:
			l1.append('7')
		elif i in {'t', 'u', 'v'}:
			l1.append('8')
		elif i in {'w', 'x', 'y', 'z'}:
			l1.append('9')
	return ''.join(l1)


def printNumber(s, n):
	arr = 'abcdefghijklmnopqrtuvwxy'
	dic = {}
	x = 2
	for i in range(0, 24, 3):
		dic[arr[i]] = x
		dic[arr[i + 1]] = x
		dic[arr[i + 2]] = x
		x += 1
	dic['s'] = 7
	dic['z'] = 9
	an = ''
	for i in s:
		an = an + str(dic[i])
	return an


def printNumber(s, n):
	(num_2, num_3, num_4) = ('abc', 'def', 'ghi')
	(num_5, num_6, num_7, num_8, num_9) = ('jkl', 'mno', 'pqrs', 'tuv', 'wxyz')
	result = ''
	for char in s:
		if char in num_2:
			result += '2'
		elif char in num_3:
			result += '3'
		elif char in num_4:
			result += '4'
		elif char in num_5:
			result += '5'
		elif char in num_6:
			result += '6'
		elif char in num_7:
			result += '7'
		elif char in num_8:
			result += '8'
		elif char in num_9:
			result += '9'
	return result


def printNumber(s, n):
	str = ''
	for char in s:
		if char in 'abc':
			str += '2'
		elif char in 'def':
			str += '3'
		elif char in 'ghi':
			str += '4'
		elif char in 'jkl':
			str += '5'
		elif char in 'mno':
			str += '6'
		elif char in 'pqrs':
			str += '7'
		elif char in 'tuv':
			str += '8'
		elif char in 'wxyz':
			str += '9'
	return str


def printNumber(s, n):
	(digit2, digit3, digit4, digit5, digit6, digit7, digit8, digit9) = ('abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz')
	d = ''
	for i in s:
		if i in digit2:
			d += '2'
		elif i in digit3:
			d += '3'
		elif i in digit4:
			d += '4'
		elif i in digit5:
			d += '5'
		elif i in digit6:
			d += '6'
		elif i in digit7:
			d += '7'
		elif i in digit8:
			d += '8'
		elif i in digit9:
			d += '9'
	return d


def printNumber(s, n):
	str_list = ['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(n):
		if s[i] == ' ':
			ans += '0'
		else:
			j = ord(s[i].upper()) - ord('A')
			ans += str_list[j][0]
	return ans


def printNumber(s, n):
	a = ''
	(two, three, four, five, six, seven, eight, nine) = ('abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz')
	for i in s:
		if i in two:
			a += '2'
		elif i in three:
			a += '3'
		elif i in four:
			a += '4'
		elif i in five:
			a += '5'
		elif i in six:
			a += '6'
		elif i in seven:
			a += '7'
		elif i in eight:
			a += '8'
		else:
			a += '9'
	return a


def printNumber(s, n):
	result = []
	s = s.upper()
	for i in s:
		if i == 'A' or i == 'B' or i == 'C':
			result.append('2')
		elif i == 'D' or i == 'E' or i == 'F':
			result.append('3')
		elif i == 'G' or i == 'H' or i == 'I':
			result.append('4')
		elif i == 'J' or i == 'K' or i == 'L':
			result.append('5')
		elif i == 'M' or i == 'N' or i == 'O':
			result.append('6')
		elif i == 'P' or i == 'Q' or i == 'R' or (i == 'S'):
			result.append('7')
		elif i == 'T' or i == 'U' or i == 'V':
			result.append('8')
		elif i == 'W' or i == 'X' or i == 'Y' or (i == 'Z'):
			result.append('9')
	string = ''
	return string.join(result)


def printNumber(s, n):
	a = 'abcdefghijklmnopqrstuvwxyz'
	v = '22233344455566677778889999'
	ans = ''
	for i in s:
		ans += v[a.index(i)]
	return ans


def printNumber(s, n):
	sr = 'abcdefghijklmnopqrstuvwxyz'
	nr = '22233344455566677778889999'
	a = ''
	for i in s:
		a += nr[sr.index(i)]
	return a


def printNumber(s, n):
	S = ''
	for i in s:
		if i in 'abc':
			S += '2'
		if i in 'def':
			S += '3'
		if i in 'ghi':
			S += '4'
		if i in 'jkl':
			S += '5'
		if i in 'mno':
			S += '6'
		if i in 'pqrs':
			S += '7'
		if i in 'tuv':
			S += '8'
		if i in 'wxyz':
			S += '9'
	return S


def printNumber(s, n):
	str1 = ['2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9']
	output = ''
	for i in range(n):
		pos = ord(s[i]) - ord('a')
		output = output + str1[pos]
	return output


def printNumber(s, n):
	s = s.upper()
	decimal = {'2': set('ABC'), '3': set('DEF'), '4': set('GHI'), '5': set('JKL'), '6': set('MNO'), '7': set('PQRS'), '8': set('TUV'), '9': set('WXYZ')}
	decimal_string = ''.join((key for char in s for (key, value) in decimal.items() if char in value))
	return decimal_string


def printNumber(s, n):
	ans = ''
	for i in s:
		if i in 'abc':
			ans += '2'
		elif i in 'def':
			ans += '3'
		elif i in 'ghi':
			ans += '4'
		elif i in 'jkl':
			ans += '5'
		elif i in 'mno':
			ans += '6'
		elif i in 'pqrs':
			ans += '7'
		elif i in 'tuv':
			ans += '8'
		else:
			ans += '9'
	return ans


def printNumber(s, n):
	store = {}
	for i in range(26):
		store[chr(i + 97)] = str(i // 3 + 2)
	store['s'] = '7'
	store['v'] = '8'
	store['y'] = '9'
	store['z'] = '9'
	output = ''
	for item in s:
		output += store[item]
	return output


def printNumber(s, n):
	map = {'abc': '2', 'def': '3', 'ghi': '4', 'jkl': '5', 'mno': '6', 'pqrs': '7', 'tuv': '8', 'wxyz': '9'}
	x = [map[j] for i in s for j in map.keys() if i in j]
	return ''.join(x)


def printNumber(s, n):
	l = ''
	for i in s.upper():
		if i == 'A' or i == 'B' or i == 'C':
			l = l + '2'
		elif i == 'D' or i == 'E' or i == 'F':
			l = l + '3'
		elif i == 'G' or i == 'H' or i == 'I':
			l = l + '4'
		elif i == 'J' or i == 'K' or i == 'L':
			l = l + '5'
		elif i == 'M' or i == 'N' or i == 'O':
			l = l + '6'
		elif i == 'P' or i == 'Q' or i == 'R' or (i == 'S'):
			l = l + '7'
		elif i == 'T' or i == 'U' or i == 'V':
			l = l + '8'
		else:
			l = l + '9'
	return l


def printNumber(s, n):
	str1 = ''
	for s in s:
		if s >= 'a' and s <= 'c':
			str1 += '2'
		elif s >= 'd' and s <= 'f':
			str1 += '3'
		elif s >= 'g' and s <= 'i':
			str1 += '4'
		elif s >= 'j' and s <= 'l':
			str1 += '5'
		elif s >= 'm' and s <= 'o':
			str1 += '6'
		elif s >= 'p' and s <= 's':
			str1 += '7'
		elif s >= 't' and s <= 'v':
			str1 += '8'
		elif s >= 'w' and s <= 'z':
			str1 += '9'
	return str1


def printNumber(s, n):
	d = {'abc': '2', 'def': '3', 'ghi': '4', 'jkl': '5', 'mno': '6', 'pqrs': '7', 'tuv': '8', 'wxyz': '9'}
	res = ''
	for c in s:
		for (k, v) in d.items():
			if c in k:
				res += v
	return res


def printNumber(s, n):
	sum1 = ''
	for i in s:
		if 97 <= ord(i) <= 99:
			sum1 += '2'
		elif 100 <= ord(i) <= 102:
			sum1 += '3'
		elif 103 <= ord(i) <= 105:
			sum1 += '4'
		elif 106 <= ord(i) <= 108:
			sum1 += '5'
		elif 109 <= ord(i) <= 111:
			sum1 += '6'
		elif 112 <= ord(i) <= 115:
			sum1 += '7'
		elif 116 <= ord(i) <= 118:
			sum1 += '8'
		elif 119 <= ord(i) <= 122:
			sum1 += '9'
	return sum1


def printNumber(s, n):
	op = ''
	for i in range(n):
		if s[i] in 'abc':
			op += '2'
		elif s[i] in 'def':
			op += '3'
		elif s[i] in 'ghi':
			op += '4'
		elif s[i] in 'jkl':
			op += '5'
		elif s[i] in 'mno':
			op += '6'
		elif s[i] in 'pqrs':
			op += '7'
		elif s[i] in 'tuv':
			op += '8'
		elif s[i] in 'wxyz':
			op += '9'
		else:
			pass
	return op


def printNumber(s, n):
	l = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
	r = ''
	for i in s:
		if i in l[0]:
			r += '2'
		elif i in l[1]:
			r += '3'
		elif i in l[2]:
			r += '4'
		elif i in l[3]:
			r += '5'
		elif i in l[4]:
			r += '6'
		elif i in l[5]:
			r += '7'
		elif i in l[6]:
			r += '8'
		elif i in l[7]:
			r += '9'
		elif i == ' ':
			r += '0'
	return r


def printNumber(s, n):
	s = s.upper()
	ans = ''
	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']
	for i in s:
		if i in two:
			ans += '2'
		elif i in three:
			ans += '3'
		elif i in four:
			ans += '4'
		elif i in five:
			ans += '5'
		elif i in six:
			ans += '6'
		elif i in seven:
			ans += '7'
		elif i in eight:
			ans += '8'
		else:
			ans += '9'
	return ans


def printNumber(s, n):
	a = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
	b = [2, 3, 4, 5, 6, 7, 8, 9]
	a1 = set(list(s))
	for i in a1:
		for j in a:
			if i in j:
				v = b[a.index(j)]
				s = s.replace(i, str(v))
				break
	return int(s)


def printNumber(s, n):
	numbers = '22233344455566677778889999'
	final_num = ''
	for char in s:
		diff = ord(char) - ord('a')
		final_num = final_num + numbers[diff]
	return final_num


def printNumber(s, n):
	nums = '22233344455566677778889999'
	ans = ''
	c = 'a'
	for i in range(n):
		temp = ord(s[i]) - ord(c)
		ans = ans + nums[temp]
	return ans


def printNumber(s, n):
	c = []
	c = s
	sum = ''
	for i in c:
		if i in 'ABCabc':
			sum += str(2)
		elif i in 'DEFdef':
			sum += str(3)
		elif i in 'GHIghi':
			sum += str(4)
		elif i in 'JKLjkl':
			sum += str(5)
		elif i in 'MNOmno':
			sum += str(6)
		elif i in 'PQRSpqrs':
			sum += str(7)
		elif i in 'TUVtuv':
			sum += str(8)
		elif i in 'WXYZwxyz':
			sum += str(9)
	return sum