Skip to content

Commit ec3e1ed

Browse files
authored
Create LetterCasePermutation.java
1 parent 152ff48 commit ec3e1ed

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//LeetCode 784. Letter Case Permutation
2+
//Question - https://leetcode.com/problems/letter-case-permutation/
3+
4+
class Solution {
5+
public List<String> letterCasePermutation(String S) {
6+
List<String> res = new ArrayList<>();
7+
helper(S, "", 0, res);
8+
return res;
9+
}
10+
11+
public void helper(String inputString, String newString, int index, List<String> l){
12+
//base condition
13+
if(inputString.length() == newString.length()){
14+
l.add(newString);
15+
return;
16+
}
17+
18+
//check if current char is a number or alphabet
19+
char currChar = inputString.charAt(index);
20+
if(Character.isDigit(currChar)){
21+
helper(inputString, newString + currChar, index + 1, l);
22+
}
23+
else{
24+
//considering lower case
25+
helper(inputString, newString + Character.toLowerCase(currChar), index + 1, l);
26+
27+
//considering upper case
28+
helper(inputString, newString + Character.toUpperCase(currChar), index + 1, l);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)