Skip to content

Commit 1f0e110

Browse files
committed
Letter Case Permutation
1 parent af96ad5 commit 1f0e110

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
''' Leetcode- https://leetcode.com/problems/letter-case-permutation/'''
2+
'''
3+
Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.
4+
5+
Return a list of all possible strings we could create. Return the output in any order.
6+
7+
8+
Input: s = "a1b2"
9+
Output: ["a1b2","a1B2","A1b2","A1B2"]
10+
'''
11+
12+
def letterCasePermutation(S):
13+
S = S.lower()
14+
lenS, ans = len(S), []
15+
def dfs(i, res=''):
16+
if i < lenS:
17+
dfs(i+1, res + S[i])
18+
if S[i].islower():
19+
dfs(i+1, res + S[i].upper())
20+
else:
21+
ans.append(res)
22+
dfs(0)
23+
return ans
24+
25+
print(letterCasePermutation("a1b2"))#['a1b2', 'a1B2', 'A1b2', 'A1B2']
26+
# T: O(M * 2^L)
27+
# S: O(L)

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# leetcode-solutions-python
22

33
- [x] [Backtracking](Backtracking)
4-
- [x] [Word Search](Backtracking/79-Word-Search.py)
4+
- [x] [Word Search](Backtracking/79-Word-Search.py)
5+
- [x] [Letter Case Permutation](Backtracking/784-letter-case-permutation.py)
6+

0 commit comments

Comments
 (0)