1. 문제 분석

  • 문제 링크: Letter Case Permutation
  • 핵심 요구사항:
    • 문자열 s가 주어질 때, 영문자는 대소문자를 변경하여 가능한 모든 조합을 생성.
    • 숫자는 변환 없이 그대로 유지.

2. 풀이 접근 방식 (Set + DFS)

별도의 정규식이나 조건문 없이, 모든 위치에서 .toUpperCase().toLowerCase()를 모두 호출하여 탐색합니다.

function letterCasePermutation(s: string): string[] {
    const set = new Set<string>()

    function dfs(curStr: string) {
        if(curStr.length === s.length) {
            set.add(curStr)
            return
        }

        const len = curStr.length
        dfs(curStr + s[len].toUpperCase())
        dfs(curStr + s[len].toLowerCase())
    }

    dfs('')
    return [...set]
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글