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..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 @@ -392,25 +392,27 @@ 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 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; } - 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 d[+digits[i] - 2]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; } ``` @@ -453,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 a15369a5b20c5..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 @@ -388,25 +388,27 @@ 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 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; } - 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 d[+digits[i] - 2]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; } ``` @@ -449,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 3c6dce34cf09c..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 @@ -1,22 +1,24 @@ 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 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; } - 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 d[+digits[i] - 2]) { + dfs(curr + ch, i + 1); + } } }; - dfs(0); + dfs('', 0); return ans; }