DEV Community

Cover image for Day 2 of 100 days dsa coding challenge
Manasi Patil
Manasi Patil

Posted on

Day 2 of 100 days dsa coding challenge

Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! πŸ’»πŸ”₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! πŸš€

100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney

Problem:

https://www.geeksforgeeks.org/problems/possible-words-from-phone-digits-1587115620/1

Possible Words From Phone Digits
Difficulty: Medium Accuracy: 52.79%

You are given a keypad (as shown in the diagram) and an array arr[ ] containing digits, your task is to list all possible words in any order which can be generated by pressing numbers in arr[] sequentially.
Note: Number 0 and 1 do not map to any letters.
You can return the words in any order, the driver code will print them in sorted order.

Examples :
Input: arr[] = [2, 3]
Output: [ad, ae, af, bd, be, bf, cd, ce, cf]
Explanation: When we press 2 and 3 total 3 x 3 = 9 possible words formed.
Input: arr[] = [2]
Output: [a, b, c]
Explanation: When we press 2 total 3 possible words formed.

Constraints:
1 ≀ arr.size() ≀ 9
0 ≀ arr[i] ≀ 9

Solution:
class Solution:
def possibleWords(self, arr):
# code here
d2l = [
[""],
[""],
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz",
]
n = len(arr)

    def gen(i=0, acc=[], ans=[]):
        if i == n:
            ans.append("".join(acc))
        else:
            for c in d2l[arr[i]]:
                acc.append(c)
                gen(i + 1)
                acc.pop()
        return ans

    return gen()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)