.Zzumbong
[leetCode/JS] 784. Letter Case Permutation 본문
문제 설명
Given a string s
, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
String s 를 받아서 개별적으로 소문자나 대문자로 변환하여, 만들 수 있는 모든 문자열을 Array로 반환한다.
순서는 상관없다.
입출력 예
Example 1:
Input: s = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]
Example 2:
Input: s = "3z4"
Output: ["3z4","3Z4"]
Constraints
1 <= s.length <= 12
s
consists of lowercase English letters, uppercase English letters, and digits.
내 솔루션
BFS
도 가능할 것같은데,DFS
가 더 쉽다고 생각해서 이걸로 풀었다.index
를+1
하며, 순환하게 된다.cur
로 글자를 만들고index
와s.length
가 같아지면answer
로push()
해서 마무리.
/**
* @param {string} s
* @return {string[]}
*/
var letterCasePermutation = function(s) {
const answer = []
const dfs = (index, cur) => {
if(index === s.length){
answer.push(cur);
return;
}
if(/^[a-z]|[A-Z]/.test(s[index])){
dfs(index+1, cur + s[index].toUpperCase());
dfs(index+1, cur + s[index].toLowerCase());
} else {
dfs(index+1, cur + s[index]);
}
}
dfs(0, '');
return answer;
};
감상평
- DFS가 익숙해지니까 이런 문제들은 점점 쉬워진다.
- leetCode
2000개
푼 고인물 개발자가 leetCode는 다크소울 같다더니 처음에 비하면 점점 발전하고 있다. 신기해라
'coding test > leetCode' 카테고리의 다른 글
[leetCocd/JS] 907. Sum of Subarray Minimums (0) | 2022.11.25 |
---|---|
[leetCode/JS] 79. Word Search (0) | 2022.11.24 |
[leetCode/JS] 14. Longest Common Prefix (0) | 2022.11.24 |
[leetCode/JS] 14. Longest Common Prefix (0) | 2022.11.24 |
[leetCode/JS] 1926. Nearest Exit from Entrance in Maz (0) | 2022.11.24 |
Comments