From 55b4fee0aaf5a7590e0882900569053120ded71c Mon Sep 17 00:00:00 2001 From: Gyunseo Lee Date: Tue, 2 Jul 2024 23:25:28 +0900 Subject: [PATCH] 2024-07-02 23:25:28 Affected files: .obsidian/workspace.json src/content/blog/leet-code-letter-combinations-of-a-phone-number.md --- .obsidian/workspace.json | 28 ++++----- ...e-letter-combinations-of-a-phone-number.md | 63 +++++++++++++++++++ 2 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 src/content/blog/leet-code-letter-combinations-of-a-phone-number.md diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index e39099809..6825d034d 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -4,16 +4,16 @@ "type": "split", "children": [ { - "id": "46a0456c3a5d08ed", + "id": "722277b9dddabeca", "type": "tabs", "children": [ { - "id": "eee94e44d418180a", + "id": "908242d88dc6887c", "type": "leaf", "state": { "type": "markdown", "state": { - "file": "src/content/blog/boj-1940-주몽.md", + "file": "src/content/blog/boj-1926-그림.md", "mode": "source", "source": false } @@ -22,16 +22,16 @@ ] }, { - "id": "722277b9dddabeca", + "id": "a13fd4920d0ad343", "type": "tabs", "children": [ { - "id": "908242d88dc6887c", + "id": "ef7666fe01f8d3aa", "type": "leaf", "state": { "type": "markdown", "state": { - "file": "src/content/blog/boj-1926-그림.md", + "file": "src/content/blog/leet-code-letter-combinations-of-a-phone-number.md", "mode": "source", "source": false } @@ -103,7 +103,7 @@ "state": { "type": "backlink", "state": { - "file": "src/content/blog/boj-1926-그림.md", + "file": "src/content/blog/leet-code-letter-combinations-of-a-phone-number.md", "collapseAll": false, "extraContext": false, "sortOrder": "alphabetical", @@ -120,7 +120,7 @@ "state": { "type": "outgoing-link", "state": { - "file": "src/content/blog/boj-1926-그림.md", + "file": "src/content/blog/leet-code-letter-combinations-of-a-phone-number.md", "linksCollapsed": false, "unlinkedCollapsed": true } @@ -143,7 +143,7 @@ "state": { "type": "outline", "state": { - "file": "src/content/blog/boj-1926-그림.md" + "file": "src/content/blog/leet-code-letter-combinations-of-a-phone-number.md" } } } @@ -166,17 +166,18 @@ "table-editor-obsidian:Advanced Tables Toolbar": false } }, - "active": "908242d88dc6887c", + "active": "ef7666fe01f8d3aa", "lastOpenFiles": [ - "src/content/blog/boj-1940-주몽.md", "src/content/blog/boj-1926-그림.md", + "src/content/blog/leet-code-minimum-path-sum.md", + "src/content/blog/leet-code-letter-combinations-of-a-phone-number.md", + "src/content/blog/boj-1940-주몽.md", "src/content/blog/install-neovim-using-aqua-on-ubuntu.md", "src/content/blog/how-to-connect-wifi-on-ubuntu-server.md", "dist/posts/aqua를-이용해-wsl-ubuntu에-neovim을-설치하자.png", "src/content/blog/manage-cli-program-version-using-aqua-proj-on-windows.md", "src/content/blog/astro-paper-4.md", "src/content/blog/implementing-a-worker-thread-pool-in-c.md", - "src/content/blog/leet-code-minimum-path-sum.md", "src/content/blog/boj-5014-스타트링크.md", "src/content/blog/boj-random-defense.md", "src/content/blog/boj-14425-문자열-집합.md", @@ -212,7 +213,6 @@ "src/content/blog/boj-1699-제곱수의-합.md", "src/content/blog/boj-1026-보물.md", "src/content/blog/computer-science-qna.md", - "src/content/blog/programmers-보호소에서-중성화한-동물.md", - "src/content/blog/programmers-즐겨찾기가-가장-많은 식당-정보-출력하기.md" + "src/content/blog/programmers-보호소에서-중성화한-동물.md" ] } \ No newline at end of file diff --git a/src/content/blog/leet-code-letter-combinations-of-a-phone-number.md b/src/content/blog/leet-code-letter-combinations-of-a-phone-number.md new file mode 100644 index 000000000..d8fd541c3 --- /dev/null +++ b/src/content/blog/leet-code-letter-combinations-of-a-phone-number.md @@ -0,0 +1,63 @@ +--- +author: Gyunseo Lee +title: "LeetCode: Letter Combinations of a Phone Number" +pubDatetime: 2024-07-02T23:16:00+09:00 +modDatetime: 2024-07-02T23:16:00+09:00 +featured: false +draft: false +tags: + - PS + - LeetCode + - Algorithms + - Brute-Force +description: Python의 기본 내장 라이브러리를 적극 활용하자... +ogImage: "" +--- + +## Table of contents + +## 들어가며 + +걸린시간: 11분 + +처음 문제를 읽고, 모든 경우의 수를 Cartesian Product로 구하면 답은 나오겠다는 생각이 들었습니다. + +## 접근 + +그리고 문제 조건을 보니깐, digits의 최대 길이가 4이어서, Python의 product 함수를 써서 카테시안 곱들을 구해도 문제에서 시간초과를 받지 않겠다는 생각을 하고 구현을 했습니다. + +## 구현 + +휴대전화 버튼의 각 숫자에 해당하는 문자들은 Dictionary를 이용해서, Global 변수로 저장해서 구현을 했습니다. + +```python +from itertools import product + +digitAlphabetMap = { + "2": ["a", "b", "c"], + "3": ["d", "e", "f"], + "4": ["g", "h", "i"], + "5": ["j", "k", "l"], + "6": ["m", "n", "o"], + "7": ["p", "q", "r", "s"], + "8": ["t", "u", "v"], + "9": ["w", "x", "y", "z"] +} + +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + stringList = [] + combinationList = [] + for d in digits: + stringList.append(digitAlphabetMap[d]) + for comb in product(*stringList): + if len(comb) == 0: + continue + combinationList.append("".join(comb)) + return combinationList +``` + +## 다른 풀이 방법 + +DFS 알고리즘을 사용해서 product 라이브러리를 사용하지 않고, 직접 카테시안 곱들을 구할 수 있습니다. +코테에서는 어떤 문제가 나올지 모르니 그렇게 구현하는 것도 연습을 해 봐야 겠습니다 ㅎㅎ 🥸