From 374bc211d0459ba3b56af9c3a9e93364eef5edb5 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 7 Jul 2024 17:01:31 +0300 Subject: [PATCH 1/2] feat: update ts solution to lc problem: No.0017 --- .../README.md | 33 ++++++++++++------- .../README_EN.md | 33 ++++++++++++------- .../Solution2.ts | 33 ++++++++++++------- 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md index 58d325892108f..1e1d1c315e7d2 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md @@ -392,25 +392,34 @@ func letterCombinations(digits string) (ans []string) { ```ts function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = []; - const t: string[] = []; - const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; - const dfs = (i: number) => { - if (i >= digits.length) { - ans.push(t.join('')); + const n = digits.length; + const map: Record = { + 2: [...'abc'], + 3: [...'def'], + 4: [...'ghi'], + 5: [...'jkl'], + 6: [...'mno'], + 7: [...'pqrs'], + 8: [...'tuv'], + 9: [...'wxyz'], + }; + + const dfs = (curr: string, start: number) => { + if (curr.length === n) { + ans.push(curr); return; } - const s = d[parseInt(digits[i]) - 2]; - for (const c of s) { - t.push(c); - dfs(i + 1); - t.pop(); + for (let i = start; i < n; i++) { + for (const ch of map[digits[i]]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; } ``` diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md index a15369a5b20c5..a512e65c77ce6 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md @@ -388,27 +388,36 @@ func letterCombinations(digits string) (ans []string) { ```ts function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = []; - const t: string[] = []; - const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; - const dfs = (i: number) => { - if (i >= digits.length) { - ans.push(t.join('')); + const n = digits.length; + const map: Record = { + 2: [...'abc'], + 3: [...'def'], + 4: [...'ghi'], + 5: [...'jkl'], + 6: [...'mno'], + 7: [...'pqrs'], + 8: [...'tuv'], + 9: [...'wxyz'], + }; + const dfs = (curr: string, start: number) => { + if (curr.length === n) { + ans.push(curr); return; } - const s = d[parseInt(digits[i]) - 2]; - for (const c of s) { - t.push(c); - dfs(i + 1); - t.pop(); + for (let i = start; i < n; i++) { + for (const ch of map[digits[i]]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; } + ``` #### Rust diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts index 3c6dce34cf09c..0dddc8f89e65a 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts @@ -1,22 +1,31 @@ function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = []; - const t: string[] = []; - const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; - const dfs = (i: number) => { - if (i >= digits.length) { - ans.push(t.join('')); + const n = digits.length; + const map: Record = { + 2: [...'abc'], + 3: [...'def'], + 4: [...'ghi'], + 5: [...'jkl'], + 6: [...'mno'], + 7: [...'pqrs'], + 8: [...'tuv'], + 9: [...'wxyz'], + }; + + const dfs = (curr: string, start: number) => { + if (curr.length === n) { + ans.push(curr); return; } - const s = d[parseInt(digits[i]) - 2]; - for (const c of s) { - t.push(c); - dfs(i + 1); - t.pop(); + for (let i = start; i < n; i++) { + for (const ch of map[digits[i]]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; } From b874f9262ed858008439e8e5bef41932f4196176 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 7 Jul 2024 17:03:51 +0300 Subject: [PATCH 2/2] feat: update js solution to lc problem: No.0017 --- .../README.md | 39 ++++++++---------- .../README_EN.md | 41 ++++++++----------- .../Solution2.js | 24 +++++------ .../Solution2.ts | 15 ++----- 4 files changed, 49 insertions(+), 70 deletions(-) diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md index 1e1d1c315e7d2..bd6f35a246a46 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md @@ -397,16 +397,9 @@ function letterCombinations(digits: string): string[] { } const ans: string[] = []; const n = digits.length; - const map: Record = { - 2: [...'abc'], - 3: [...'def'], - 4: [...'ghi'], - 5: [...'jkl'], - 6: [...'mno'], - 7: [...'pqrs'], - 8: [...'tuv'], - 9: [...'wxyz'], - }; + const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [ + ...x, + ]); const dfs = (curr: string, start: number) => { if (curr.length === n) { @@ -414,7 +407,7 @@ function letterCombinations(digits: string): string[] { return; } for (let i = start; i < n; i++) { - for (const ch of map[digits[i]]) { + for (const ch of d[+digits[i] - 2]) { dfs(curr + ch, i + 1); } } @@ -462,25 +455,25 @@ impl Solution { * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = []; - const t = []; - const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; - const dfs = i => { - if (i >= digits.length) { - ans.push(t.join('')); + const n = digits.length; + const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]); + + const dfs = (curr, start) => { + if (curr.length === n) { + ans.push(curr); return; } - const s = d[parseInt(digits[i]) - 2]; - for (const c of s) { - t.push(c); - dfs(i + 1); - t.pop(); + for (let i = start; i < n; i++) { + for (const ch of map[+digits[i] - 2]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; }; ``` diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md index a512e65c77ce6..407b70a120d52 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md @@ -393,23 +393,17 @@ function letterCombinations(digits: string): string[] { } const ans: string[] = []; const n = digits.length; - const map: Record = { - 2: [...'abc'], - 3: [...'def'], - 4: [...'ghi'], - 5: [...'jkl'], - 6: [...'mno'], - 7: [...'pqrs'], - 8: [...'tuv'], - 9: [...'wxyz'], - }; + const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [ + ...x, + ]); + const dfs = (curr: string, start: number) => { if (curr.length === n) { ans.push(curr); return; } for (let i = start; i < n; i++) { - for (const ch of map[digits[i]]) { + for (const ch of d[+digits[i] - 2]) { dfs(curr + ch, i + 1); } } @@ -417,7 +411,6 @@ function letterCombinations(digits: string): string[] { dfs('', 0); return ans; } - ``` #### Rust @@ -458,25 +451,25 @@ impl Solution { * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = []; - const t = []; - const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; - const dfs = i => { - if (i >= digits.length) { - ans.push(t.join('')); + const n = digits.length; + const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]); + + const dfs = (curr, start) => { + if (curr.length === n) { + ans.push(curr); return; } - const s = d[parseInt(digits[i]) - 2]; - for (const c of s) { - t.push(c); - dfs(i + 1); - t.pop(); + for (let i = start; i < n; i++) { + for (const ch of map[+digits[i] - 2]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; }; ``` diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js index 3ba4721ef1457..480c6f03e4ee6 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js @@ -3,24 +3,24 @@ * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = []; - const t = []; - const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; - const dfs = i => { - if (i >= digits.length) { - ans.push(t.join('')); + const n = digits.length; + const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [...x]); + + const dfs = (curr, start) => { + if (curr.length === n) { + ans.push(curr); return; } - const s = d[parseInt(digits[i]) - 2]; - for (const c of s) { - t.push(c); - dfs(i + 1); - t.pop(); + for (let i = start; i < n; i++) { + for (const ch of map[+digits[i] - 2]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; }; diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts index 0dddc8f89e65a..34e4bc83d9846 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts @@ -4,16 +4,9 @@ function letterCombinations(digits: string): string[] { } const ans: string[] = []; const n = digits.length; - const map: Record = { - 2: [...'abc'], - 3: [...'def'], - 4: [...'ghi'], - 5: [...'jkl'], - 6: [...'mno'], - 7: [...'pqrs'], - 8: [...'tuv'], - 9: [...'wxyz'], - }; + const d: string[][] = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'].map(x => [ + ...x, + ]); const dfs = (curr: string, start: number) => { if (curr.length === n) { @@ -21,7 +14,7 @@ function letterCombinations(digits: string): string[] { return; } for (let i = start; i < n; i++) { - for (const ch of map[digits[i]]) { + for (const ch of d[+digits[i] - 2]) { dfs(curr + ch, i + 1); } }