Skip to content

Commit 0920602

Browse files
committed
solve problem Letter Case Permutation
1 parent 50b757f commit 0920602

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ All solutions will be accepted!
9595
|697|[Degree Of An Array](https://leetcode-cn.com/problems/degree-of-an-array/description/)|[java/py/js](./algorithms/DegreeOfAnArray)|Easy|
9696
|671|[Second Minimum Node In A Binary Tree](https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/description/)|[java/py/js](./algorithms/SecondMinimunNodeInABinaryTree)|Easy|
9797
|242|[Valid Anagram](https://leetcode-cn.com/problems/valid-anagram/description/)|[java/py/js](./algorithms/ValidAnagram)|Easy|
98+
|784|[Letter Case Permutation](https://leetcode-cn.com/problems/letter-case-permutation/description/)|[java/py/js](./algorithms/LetterCasePermutation)|Easy|
9899

99100
# Database
100101
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Letter Case Permutation
2+
This problem is easy to solve by set
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<String> letterCasePermutation(String S) {
3+
Set<String> res = new HashSet<String>();
4+
5+
for (char c : S.toCharArray()) {
6+
String s = String.valueOf(c);
7+
if (res.size() == 0) {
8+
res.add(s.toUpperCase());
9+
res.add(s.toLowerCase());
10+
} else {
11+
Set<String> temp = new HashSet<String>();
12+
for (String v : res) {
13+
temp.add(v + s.toUpperCase());
14+
temp.add(v + s.toLowerCase());
15+
}
16+
res = temp;
17+
}
18+
}
19+
20+
res.add(S);
21+
22+
List<String> result = new ArrayList<String>();
23+
result.addAll(res);
24+
return result;
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} S
3+
* @return {string[]}
4+
*/
5+
var letterCasePermutation = function(S) {
6+
let res = new Set()
7+
8+
S.split('').forEach(c => {
9+
if (res.size === 0) {
10+
res.add(c.toUpperCase())
11+
res.add(c.toLowerCase())
12+
} else {
13+
temp = new Set()
14+
res.forEach(v => {
15+
temp.add(v + c.toUpperCase())
16+
temp.add(v + c.toLowerCase())
17+
})
18+
res = temp
19+
}
20+
})
21+
22+
res.add(S)
23+
return Array.from(res)
24+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def letterCasePermutation(self, S):
3+
"""
4+
:type S: str
5+
:rtype: List[str]
6+
"""
7+
res = set()
8+
9+
for c in S:
10+
if len(res) == 0:
11+
res.add(c.upper())
12+
res.add(c.lower())
13+
else:
14+
temp = set()
15+
for j in res:
16+
temp.add(j + c.upper())
17+
temp.add(j + c.lower())
18+
res = temp
19+
20+
res.add(S)
21+
return list(res)

0 commit comments

Comments
 (0)