Skip to content

Commit

Permalink
Create phonenum-letter-comb.py
Browse files Browse the repository at this point in the history
finds all combinations of letters that can be formed turning a phone number into each numbers character representation. Very slow for longer nums as it increases exponentially
  • Loading branch information
gabedonnan authored Jan 14, 2023
1 parent fedd5a4 commit 61ad813
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions phonenum-letter-comb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
numflag = (digits != "") #Checks if digits not empty
inp = [digits]
output = []
endindex = 0
tempdigit = "2"
tempstr = ""
tempindex = 0
while numflag:
numflag = False
inp = inp[endindex:]
print(inp)
endindex = len(inp)
for s in inp:
if s[-1].isdigit():
for i in range(len(s)):
if s[i].isdigit():
numflag = True
tempdigit = s[i]
tempindex = i
break
tempstr = self.chToStr(tempdigit)
for tempch in tempstr:
inp.append(s[:tempindex] + tempch + s[tempindex+1:])
else:
if s not in output:
output.append(s)
return output

def chToStr(self, ch):
if ch == "2":
return "abc"
elif ch == "3":
return "def"
elif ch == "4":
return "ghi"
elif ch == "5":
return "jkl"
elif ch == "6":
return "mno"
elif ch == "7":
return "pqrs"
elif ch == "8":
return "tuv"
elif ch == "9":
return "wxyz"

0 comments on commit 61ad813

Please sign in to comment.