|
5 | 5 | ## 题目描述
|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
| 8 | + |
8 | 9 | <p>在老式手机上,用户通过数字键盘输入,手机将提供与这些数字相匹配的单词列表。每个数字映射到0至4个字母。给定一个数字序列,实现一个算法来返回匹配单词的列表。你会得到一张含有有效单词的列表。映射如下图所示:</p>
|
9 | 10 | 
|
10 | 11 | <p><strong>示例 1:</strong></p>
|
|
26 | 27 |
|
27 | 28 | <!-- 这里可写通用的实现逻辑 -->
|
28 | 29 |
|
| 30 | +**方法一:逆向思维** |
| 31 | + |
| 32 | +我们考虑一种正向的解法,遍历字符串 $num$ 中的每个数字,将其映射到对应的字母,然后将所有的字母组合起来,得到所有可能的单词,再与给定的单词列表进行比较,若单词在列表中,则将其加入答案。这种解法的时间复杂度为 $O(4^n)$,其中 $n$ 为字符串 $num$ 的长度,显然会超时。 |
| 33 | + |
| 34 | +我们不妨考虑逆向的解法,遍历给定的单词列表,对于每个单词 $w$,判断其是否能够由字符串 $num$ 中的数字组成。若能够组成,则将其加入答案。那么问题的关键在于如何判断一个单词是否能够由字符串 $num$ 中的数字组成。我们只需要遍历单词 $w$ 的每个字母,将其还原为对应的数字,逐个与字符串 $num$ 中的数字进行比较,若相同,则说明单词 $w$ 可以由字符串 $num$ 中的数字组成。 |
| 35 | + |
| 36 | +时间复杂度 $O(m \times n)$,空间复杂度 $O(C)$。其中 $m$ 和 $n$ 分别是单词列表的长度和字符串 $num$ 的长度;而 $C$ 为字符集大小,本题中字符集大小为 $26$。 |
| 37 | + |
29 | 38 | <!-- tabs:start -->
|
30 | 39 |
|
31 | 40 | ### **Python3**
|
32 | 41 |
|
33 | 42 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
34 | 43 |
|
35 | 44 | ```python
|
| 45 | +class Solution: |
| 46 | + def getValidT9Words(self, num: str, words: List[str]) -> List[str]: |
| 47 | + def check(w: str) -> bool: |
| 48 | + return all(d[c] == num[i] for i, c in enumerate(w)) |
36 | 49 |
|
| 50 | + d = {c: d for c, d in zip(ascii_lowercase, "22233344455566677778889999")} |
| 51 | + return [w for w in words if check(w)] |
| 52 | +``` |
| 53 | + |
| 54 | +```python |
| 55 | +class Solution: |
| 56 | + def getValidT9Words(self, num: str, words: List[str]) -> List[str]: |
| 57 | + trans = str.maketrans(ascii_lowercase, "22233344455566677778889999") |
| 58 | + return [w for w in words if w.translate(trans) == num] |
37 | 59 | ```
|
38 | 60 |
|
39 | 61 | ### **Java**
|
40 | 62 |
|
41 | 63 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
42 | 64 |
|
43 | 65 | ```java
|
| 66 | +class Solution { |
| 67 | + public List<String> getValidT9Words(String num, String[] words) { |
| 68 | + String s = "22233344455566677778889999"; |
| 69 | + int[] d = new int[26]; |
| 70 | + for (int i = 0; i < 26; ++i) { |
| 71 | + d[i] = s.charAt(i); |
| 72 | + } |
| 73 | + List<String> ans = new ArrayList<>(); |
| 74 | + int n = num.length(); |
| 75 | + for (String w : words) { |
| 76 | + boolean ok = true; |
| 77 | + for (int i = 0; i < n; ++i) { |
| 78 | + if (d[w.charAt(i) - 'a'] != num.charAt(i)) { |
| 79 | + ok = false; |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + if (ok) { |
| 84 | + ans.add(w); |
| 85 | + } |
| 86 | + } |
| 87 | + return ans; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### **C++** |
| 93 | + |
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + vector<string> getValidT9Words(string num, vector<string>& words) { |
| 98 | + string s = "22233344455566677778889999"; |
| 99 | + int d[26]; |
| 100 | + for (int i = 0; i < 26; ++i) { |
| 101 | + d[i] = s[i]; |
| 102 | + } |
| 103 | + vector<string> ans; |
| 104 | + int n = num.size(); |
| 105 | + for (auto& w : words) { |
| 106 | + bool ok = true; |
| 107 | + for (int i = 0; i < n; ++i) { |
| 108 | + if (d[w[i] - 'a'] != num[i]) { |
| 109 | + ok = false; |
| 110 | + } |
| 111 | + } |
| 112 | + if (ok) { |
| 113 | + ans.emplace_back(w); |
| 114 | + } |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | +}; |
| 119 | +``` |
| 120 | +
|
| 121 | +### **Go** |
| 122 | +
|
| 123 | +```go |
| 124 | +func getValidT9Words(num string, words []string) (ans []string) { |
| 125 | + s := "22233344455566677778889999" |
| 126 | + d := [26]rune{} |
| 127 | + for i, c := range s { |
| 128 | + d[i] = c |
| 129 | + } |
| 130 | + for _, w := range words { |
| 131 | + ok := true |
| 132 | + for i, c := range w { |
| 133 | + if d[c-'a'] != rune(num[i]) { |
| 134 | + ok = false |
| 135 | + break |
| 136 | + } |
| 137 | + } |
| 138 | + if ok { |
| 139 | + ans = append(ans, w) |
| 140 | + } |
| 141 | + } |
| 142 | + return |
| 143 | +} |
| 144 | +``` |
44 | 145 |
|
| 146 | +### **TypeScript** |
| 147 | + |
| 148 | +```ts |
| 149 | +function getValidT9Words(num: string, words: string[]): string[] { |
| 150 | + const s = '22233344455566677778889999'; |
| 151 | + const d: string[] = Array(26); |
| 152 | + for (let i = 0; i < 26; ++i) { |
| 153 | + d[i] = s[i]; |
| 154 | + } |
| 155 | + const ans: string[] = []; |
| 156 | + const n = num.length; |
| 157 | + for (const w of words) { |
| 158 | + let ok = true; |
| 159 | + for (let i = 0; i < n; ++i) { |
| 160 | + if (d[w[i].charCodeAt(0) - 97] !== num[i]) { |
| 161 | + ok = false; |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + if (ok) { |
| 166 | + ans.push(w); |
| 167 | + } |
| 168 | + } |
| 169 | + return ans; |
| 170 | +} |
45 | 171 | ```
|
46 | 172 |
|
47 | 173 | ### **...**
|
|
0 commit comments