Skip to content

Commit 568415b

Browse files
committed
[LeetCode Sync] Runtime - 3 ms (89.59%), Memory - 18.7 MB (69.06%)
1 parent 6bdb021 commit 568415b

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<p>Given a string <code>s</code>, you&nbsp;can transform every letter individually to be lowercase or uppercase to create another string.</p>
2+
3+
<p>Return <em>a list of all possible strings we could create</em>. Return the output in <strong>any order</strong>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> s = &quot;a1b2&quot;
10+
<strong>Output:</strong> [&quot;a1b2&quot;,&quot;a1B2&quot;,&quot;A1b2&quot;,&quot;A1B2&quot;]
11+
</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> s = &quot;3z4&quot;
17+
<strong>Output:</strong> [&quot;3z4&quot;,&quot;3Z4&quot;]
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>1 &lt;= s.length &lt;= 12</code></li>
25+
<li><code>s</code> consists of lowercase English letters, uppercase English letters, and digits.</li>
26+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def letterCasePermutation(self, s: str) -> List[str]:
3+
# result = []
4+
# n = sum(char.isalpha() for char in s)
5+
6+
# for i in range(1 << n):
7+
# j, tmp = 0, []
8+
# for char in s:
9+
# if char.isalpha():
10+
# char = char.lower() if (i >> j) & 1 else char.upper()
11+
# j += 1
12+
# tmp.append(char)
13+
# result.append("".join(tmp))
14+
15+
# return result
16+
17+
result = [""]
18+
for char in s:
19+
if char.isdigit():
20+
result = [rchar + char for rchar in result]
21+
else:
22+
result = [rchar + val for rchar in result for val in (char.upper(), char.lower())]
23+
24+
return result

0 commit comments

Comments
 (0)