-
Notifications
You must be signed in to change notification settings - Fork 0
/
L784.java
38 lines (36 loc) · 1.51 KB
/
L784.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
class Solution784 {
class Solution {
/**
* 784. Letter Case Permutation https://leetcode.com/problems/letter-case-permutation/description/
*
* @param S String
* Input string
* @return List of strings
* @timeComplexity O(2 ^ n) where n is the number of chars in input string
* @spaceComplexity O(2 ^ n) Where n is the number of chars in input string
*/
public List<String> letterCasePermutation(String S) {
Set<String> resultSet = new HashSet<>();
permuteTree(S, "", 0, resultSet);
return resultSet.stream().collect(Collectors.toList());
}
private void permuteTree(String s, String currentPrefix, int currentIndex, Set<String> result) {
if (currentIndex >= s.length()) {
result.add(currentPrefix);
return;
}
if (s.charAt(currentIndex) <= '9') {
// Do nothing
} else if (s.charAt(currentIndex) <= 'Z') {
permuteTree(s, currentPrefix + (char) (s.charAt(currentIndex) + ('a' - 'A')), currentIndex + 1, result);
} else {
permuteTree(s, currentPrefix + (char) (s.charAt(currentIndex) - ('a' - 'A')), currentIndex + 1, result);
}
permuteTree(s, currentPrefix + s.charAt(currentIndex), currentIndex + 1, result);
}
}
}