-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path784_Letter_Case_Permutation.py
More file actions
62 lines (47 loc) · 1.62 KB
/
784_Letter_Case_Permutation.py
File metadata and controls
62 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
784. Letter Case Permutation
Medium
4.4K
154
Companies
Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
Example 1:
Input: s = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]
Example 2:
Input: s = "3z4"
Output: ["3z4","3Z4"]
Constraints:
1 <= s.length <= 12
s consists of lowercase English letters, uppercase English letters, and digits.
'''
class Solution:
def letterCasePermutation(self, s: str) -> List[str]:
#backtracking
current = ""
result = []
def backtrack(s,current,index):
# print(f'result: {result} current: {current}') #check this for better result
if len(current)==len(s):
return result.append(current)
if s[index].isalpha():
backtrack(s,current+s[index].lower(),index+1)
backtrack(s,current+s[index].upper(),index+1)
else:
backtrack(s,current+s[index],index+1)
backtrack(s,current,0)
return result
#iterative
# result = ['']
# for i in s:
# current=[]
# for r in result:
# if i.isalpha():
# current.append(r + i.lower())
# current.append(r + i.upper())
# else:
# current.append(r + i)
# print(current) # check this for better understanding on how iterative approach works
# result = current
# return result