-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
fedd5a4
commit 61ad813
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |