Skip to content

Commit 542f13f

Browse files
committed
784_Letter_Case_Permutation
1 parent 05250ef commit 542f13f

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Also, there are open source implementations for basic data structs and algorithm
184184
| 760 | [Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/760_Find_Anagram_Mappings.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/760_Find_Anagram_Mappings.java) | Hash table with A's (val, index), O(n) and O(n) |
185185
| 766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/766_Toeplitz_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/766_Toeplitz_Matrix.java) | Check from top left to bottom right, i,j == i + 1, j + 1. |
186186
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
187+
| 784 | [Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/784_Letter_Case_Permutation.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/784_Letter_Case_Permutation.java) | Note that this is a 2^n problem. 1. Recursively generate result with previous result <br> 2. Bin Mask, number of zeor equal to number of alpha<br>3. Python build in product. |
187188
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |
188189
| 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/811_Subdomain_Visit_Count.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/811_Subdomain_Visit_Count.java) | String split and HashMap, O(n) and O(n) |
189190
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |

java/784_Letter_Case_Permutation.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
public List<String> letterCasePermutation(String S) {
3+
List<StringBuilder> ans = new ArrayList();
4+
ans.add(new StringBuilder());
5+
6+
for (char c: S.toCharArray()) {
7+
int n = ans.size();
8+
if (Character.isLetter(c)) {
9+
for (int i = 0; i < n; ++i) {
10+
ans.add(new StringBuilder(ans.get(i)));
11+
ans.get(i).append(Character.toLowerCase(c));
12+
ans.get(n + i).append(Character.toUpperCase(c));
13+
}
14+
} else {
15+
for (int i = 0; i < n; ++i)
16+
ans.get(i).append(c);
17+
}
18+
}
19+
20+
List<String> finalans = new ArrayList();
21+
for (StringBuilder sb: ans)
22+
finalans.add(sb.toString());
23+
return finalans;
24+
}
25+
26+
/*public List<String> letterCasePermutation(String S) {
27+
int B = 0;
28+
for (char c: S.toCharArray())
29+
if (Character.isLetter(c))
30+
B++;
31+
32+
List<String> ans = new ArrayList();
33+
34+
for (int bits = 0; bits < 1<<B; bits++) {
35+
int b = 0;
36+
StringBuilder word = new StringBuilder();
37+
for (char letter: S.toCharArray()) {
38+
if (Character.isLetter(letter)) {
39+
if (((bits >> b++) & 1) == 1)
40+
word.append(Character.toLowerCase(letter));
41+
else
42+
word.append(Character.toUpperCase(letter));
43+
} else {
44+
word.append(letter);
45+
}
46+
}
47+
48+
ans.add(word.toString());
49+
}
50+
51+
return ans;
52+
53+
}*/
54+
}

python/784_Letter_Case_Permutation.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
# def letterCasePermutation(self, S):
3+
# ans = [[]]
4+
5+
# for char in S:
6+
# n = len(ans)
7+
# if char.isalpha():
8+
# # Double the ans
9+
# for i in xrange(n):
10+
# ans.append(ans[i][:])
11+
# ans[i].append(char.lower())
12+
# ans[n + i].append(char.upper())
13+
# else:
14+
# # Normal append
15+
# for i in xrange(n):
16+
# ans[i].append(char)
17+
18+
# return map("".join, ans)
19+
20+
def letterCasePermutation(self, S):
21+
B = sum(letter.isalpha() for letter in S)
22+
ans = []
23+
24+
for bits in xrange(1 << B):
25+
b = 0
26+
word = []
27+
for letter in S:
28+
if letter.isalpha():
29+
if (bits >> b) & 1:
30+
word.append(letter.lower())
31+
else:
32+
word.append(letter.upper())
33+
34+
b += 1
35+
else:
36+
word.append(letter)
37+
38+
ans.append("".join(word))
39+
return ans

0 commit comments

Comments
 (0)