Skip to content

Commit 64f4908

Browse files
committed
feat: add solutions to lc problem: No.0784
No.0784.Letter Case Permutation
1 parent 04c6efe commit 64f4908

File tree

3 files changed

+205
-9
lines changed

3 files changed

+205
-9
lines changed

solution/0700-0799/0784.Letter Case Permutation/README.md

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949

5050
时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
5151

52+
**方法二:二进制枚举**
53+
54+
对于一个字母,我们可以将其转换为大写或小写,因此对于每个字母,我们可以使用一个二进制位表示其转换的方案,即 $0$ 表示大写,而 $1$ 表示小写。
55+
56+
我们先统计字符串 $s$ 中字母的个数,记为 $n$,那么一共有 $2^n$ 种转换方案,我们可以使用二进制数的每一位表示每个字母的转换方案,从 $0$ 到 $2^n-1$ 进行枚举。
57+
58+
具体地,我们可以使用一个变量 $i$ 表示当前枚举到的二进制数,其中 $i$ 的第 $j$ 位表示第 $j$ 个字母的转换方案。即 $i$ 的第 $j$ 位为 $0$ 表示第 $j$ 个字母转换为大写,而 $i$ 的第 $j$ 位为 $1$ 表示第 $j$ 个字母转换为小写。
59+
60+
时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
61+
5262
<!-- tabs:start -->
5363

5464
### **Python3**
@@ -73,6 +83,22 @@ class Solution:
7383
return ans
7484
```
7585

86+
```python
87+
class Solution:
88+
def letterCasePermutation(self, s: str) -> List[str]:
89+
ans = []
90+
n = sum(c.isalpha() for c in s)
91+
for i in range(1 << n):
92+
j, t = 0, []
93+
for c in s:
94+
if c.isalpha():
95+
c = c.lower() if (i >> j) & 1 else c.upper()
96+
j += 1
97+
t.append(c)
98+
ans.append(''.join(t))
99+
return ans
100+
```
101+
76102
### **Java**
77103

78104
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -102,6 +128,34 @@ class Solution {
102128
}
103129
```
104130

131+
```java
132+
class Solution {
133+
public List<String> letterCasePermutation(String s) {
134+
int n = 0;
135+
for (int i = 0; i < s.length(); ++i) {
136+
if (s.charAt(i) >= 'A') {
137+
++n;
138+
}
139+
}
140+
List<String> ans = new ArrayList<>();
141+
for (int i = 0; i < 1 << n; ++i) {
142+
int j = 0;
143+
StringBuilder t = new StringBuilder();
144+
for (int k = 0; k < s.length(); ++k) {
145+
char x = s.charAt(k);
146+
if (x >= 'A') {
147+
x = ((i >> j) & 1) == 1 ? Character.toUpperCase(x) : Character.toLowerCase(x);
148+
++j;
149+
}
150+
t.append(x);
151+
}
152+
ans.add(t.toString());
153+
}
154+
return ans;
155+
}
156+
}
157+
```
158+
105159
### **C++**
106160

107161
```cpp
@@ -126,13 +180,35 @@ public:
126180
};
127181
```
128182
183+
```cpp
184+
class Solution {
185+
public:
186+
vector<string> letterCasePermutation(string s) {
187+
int n = 0;
188+
for (char c : s) if (isalpha(c)) ++n;
189+
vector<string> ans;
190+
for (int i = 0; i < 1 << n; ++i) {
191+
int j = 0;
192+
string t;
193+
for (char c : s) {
194+
if (isalpha(c)) {
195+
c = (i >> j & 1) ? toupper(c) : tolower(c);
196+
++j;
197+
}
198+
t += c;
199+
}
200+
ans.emplace_back(t);
201+
}
202+
return ans;
203+
}
204+
};
205+
```
206+
129207
### **Go**
130208

131209
```go
132-
func letterCasePermutation(s string) []string {
133-
ans := []string{}
210+
func letterCasePermutation(s string) (ans []string) {
134211
t := []byte(s)
135-
136212
var dfs func(int)
137213
dfs = func(i int) {
138214
if i >= len(t) {
@@ -151,6 +227,34 @@ func letterCasePermutation(s string) []string {
151227
}
152228
```
153229

230+
```go
231+
func letterCasePermutation(s string) (ans []string) {
232+
n := 0
233+
for _, c := range s {
234+
if c >= 'A' {
235+
n++
236+
}
237+
}
238+
for i := 0; i < 1<<n; i++ {
239+
j := 0
240+
t := []rune{}
241+
for _, c := range s {
242+
if c >= 'A' {
243+
if ((i >> j) & 1) == 1 {
244+
c = unicode.ToUpper(c)
245+
} else {
246+
c = unicode.ToLower(c)
247+
}
248+
j++
249+
}
250+
t = append(t, c)
251+
}
252+
ans = append(ans, string(t))
253+
}
254+
return ans
255+
}
256+
```
257+
154258
### **TypeScript**
155259

156260
```ts

solution/0700-0799/0784.Letter Case Permutation/README_EN.md

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ class Solution:
5757
return ans
5858
```
5959

60+
```python
61+
class Solution:
62+
def letterCasePermutation(self, s: str) -> List[str]:
63+
ans = []
64+
n = sum(c.isalpha() for c in s)
65+
for i in range(1 << n):
66+
j, t = 0, []
67+
for c in s:
68+
if c.isalpha():
69+
c = c.lower() if (i >> j) & 1 else c.upper()
70+
j += 1
71+
t.append(c)
72+
ans.append(''.join(t))
73+
return ans
74+
```
75+
6076
### **Java**
6177

6278
```java
@@ -84,6 +100,34 @@ class Solution {
84100
}
85101
```
86102

103+
```java
104+
class Solution {
105+
public List<String> letterCasePermutation(String s) {
106+
int n = 0;
107+
for (int i = 0; i < s.length(); ++i) {
108+
if (s.charAt(i) >= 'A') {
109+
++n;
110+
}
111+
}
112+
List<String> ans = new ArrayList<>();
113+
for (int i = 0; i < 1 << n; ++i) {
114+
int j = 0;
115+
StringBuilder t = new StringBuilder();
116+
for (int k = 0; k < s.length(); ++k) {
117+
char x = s.charAt(k);
118+
if (x >= 'A') {
119+
x = ((i >> j) & 1) == 1 ? Character.toUpperCase(x) : Character.toLowerCase(x);
120+
++j;
121+
}
122+
t.append(x);
123+
}
124+
ans.add(t.toString());
125+
}
126+
return ans;
127+
}
128+
}
129+
```
130+
87131
### **C++**
88132

89133
```cpp
@@ -108,13 +152,35 @@ public:
108152
};
109153
```
110154
155+
```cpp
156+
class Solution {
157+
public:
158+
vector<string> letterCasePermutation(string s) {
159+
int n = 0;
160+
for (char c : s) if (isalpha(c)) ++n;
161+
vector<string> ans;
162+
for (int i = 0; i < 1 << n; ++i) {
163+
int j = 0;
164+
string t;
165+
for (char c : s) {
166+
if (isalpha(c)) {
167+
c = (i >> j & 1) ? toupper(c) : tolower(c);
168+
++j;
169+
}
170+
t += c;
171+
}
172+
ans.emplace_back(t);
173+
}
174+
return ans;
175+
}
176+
};
177+
```
178+
111179
### **Go**
112180

113181
```go
114-
func letterCasePermutation(s string) []string {
115-
ans := []string{}
182+
func letterCasePermutation(s string) (ans []string) {
116183
t := []byte(s)
117-
118184
var dfs func(int)
119185
dfs = func(i int) {
120186
if i >= len(t) {
@@ -133,6 +199,34 @@ func letterCasePermutation(s string) []string {
133199
}
134200
```
135201

202+
```go
203+
func letterCasePermutation(s string) (ans []string) {
204+
n := 0
205+
for _, c := range s {
206+
if c >= 'A' {
207+
n++
208+
}
209+
}
210+
for i := 0; i < 1<<n; i++ {
211+
j := 0
212+
t := []rune{}
213+
for _, c := range s {
214+
if c >= 'A' {
215+
if ((i >> j) & 1) == 1 {
216+
c = unicode.ToUpper(c)
217+
} else {
218+
c = unicode.ToLower(c)
219+
}
220+
j++
221+
}
222+
t = append(t, c)
223+
}
224+
ans = append(ans, string(t))
225+
}
226+
return ans
227+
}
228+
```
229+
136230
### **TypeScript**
137231

138232
```ts

solution/0700-0799/0784.Letter Case Permutation/Solution.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
func letterCasePermutation(s string) []string {
2-
ans := []string{}
1+
func letterCasePermutation(s string) (ans []string) {
32
t := []byte(s)
4-
53
var dfs func(int)
64
dfs = func(i int) {
75
if i >= len(t) {

0 commit comments

Comments
 (0)