|
72 | 72 | <li><code>words[i]</code> 由小写英文字母组成</li>
|
73 | 73 | </ul>
|
74 | 74 |
|
75 |
| - |
76 | 75 | ## 解法
|
77 | 76 |
|
78 | 77 | <!-- 这里可写通用的实现逻辑 -->
|
|
84 | 83 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
85 | 84 |
|
86 | 85 | ```python
|
87 |
| - |
| 86 | +class Solution: |
| 87 | + def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: |
| 88 | + def count(word): |
| 89 | + counter = [0] * 26 |
| 90 | + for c in word: |
| 91 | + counter[ord(c) - ord('a')] += 1 |
| 92 | + return counter |
| 93 | + |
| 94 | + def check(counter1, counter2): |
| 95 | + for i in range(26): |
| 96 | + if counter1[i] > counter2[i]: |
| 97 | + return False |
| 98 | + return True |
| 99 | + |
| 100 | + counter = count(c.lower() for c in licensePlate if c.isalpha()) |
| 101 | + ans, n = None, 16 |
| 102 | + for word in words: |
| 103 | + if n <= len(word): |
| 104 | + continue |
| 105 | + t = count(word) |
| 106 | + if check(counter, t): |
| 107 | + n = len(word) |
| 108 | + ans = word |
| 109 | + return ans |
88 | 110 | ```
|
89 | 111 |
|
90 | 112 | ### **Java**
|
91 | 113 |
|
92 | 114 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
93 | 115 |
|
94 | 116 | ```java
|
| 117 | +class Solution { |
| 118 | + public String shortestCompletingWord(String licensePlate, String[] words) { |
| 119 | + int[] counter = count(licensePlate.toLowerCase()); |
| 120 | + String ans = null; |
| 121 | + int n = 16; |
| 122 | + for (String word : words) { |
| 123 | + if (n <= word.length()) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + int[] t = count(word); |
| 127 | + if (check(counter, t)) { |
| 128 | + n = word.length(); |
| 129 | + ans = word; |
| 130 | + } |
| 131 | + } |
| 132 | + return ans; |
| 133 | + } |
| 134 | + |
| 135 | + private int[] count(String word) { |
| 136 | + int[] counter = new int[26]; |
| 137 | + for (char c : word.toCharArray()) { |
| 138 | + if (Character.isLetter(c)) { |
| 139 | + ++counter[c - 'a']; |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + return counter; |
| 144 | + } |
| 145 | + |
| 146 | + private boolean check(int[] counter1, int[] counter2) { |
| 147 | + for (int i = 0; i < 26; ++i) { |
| 148 | + if (counter1[i] > counter2[i]) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + return true; |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### **C++** |
| 158 | + |
| 159 | +```cpp |
| 160 | +class Solution { |
| 161 | +public: |
| 162 | + string shortestCompletingWord(string licensePlate, vector<string>& words) { |
| 163 | + vector<int> counter = count(licensePlate); |
| 164 | + int n = 16; |
| 165 | + string ans; |
| 166 | + for (auto& word : words) |
| 167 | + { |
| 168 | + if (n <= word.size()) continue; |
| 169 | + vector<int> t = count(word); |
| 170 | + if (check(counter, t)) |
| 171 | + { |
| 172 | + n = word.size(); |
| 173 | + ans = word; |
| 174 | + } |
| 175 | + } |
| 176 | + return ans; |
| 177 | + } |
| 178 | + |
| 179 | + vector<int> count(string& word) { |
| 180 | + vector<int> counter(26); |
| 181 | + for (char& c : word) |
| 182 | + if (isalpha(c)) |
| 183 | + ++counter[tolower(c) - 'a']; |
| 184 | + return counter; |
| 185 | + } |
| 186 | + |
| 187 | + bool check(vector<int>& counter1, vector<int>& counter2) { |
| 188 | + for (int i = 0; i < 26; ++i) |
| 189 | + if (counter1[i] > counter2[i]) |
| 190 | + return false; |
| 191 | + return true; |
| 192 | + } |
| 193 | +}; |
| 194 | +``` |
95 | 195 |
|
| 196 | +### **Go** |
| 197 | +
|
| 198 | +```go |
| 199 | +func shortestCompletingWord(licensePlate string, words []string) string { |
| 200 | + count := func(word string) []int { |
| 201 | + counter := make([]int, 26) |
| 202 | + for _, c := range word { |
| 203 | + if unicode.IsLetter(c) { |
| 204 | + counter[c-'a']++ |
| 205 | + } |
| 206 | + } |
| 207 | + return counter |
| 208 | + } |
| 209 | +
|
| 210 | + check := func(cnt1, cnt2 []int) bool { |
| 211 | + for i := 0; i < 26; i++ { |
| 212 | + if cnt1[i] > cnt2[i] { |
| 213 | + return false |
| 214 | + } |
| 215 | + } |
| 216 | + return true |
| 217 | + } |
| 218 | +
|
| 219 | + counter := count(strings.ToLower(licensePlate)) |
| 220 | + var ans string |
| 221 | + n := 16 |
| 222 | + for _, word := range words { |
| 223 | + if n <= len(word) { |
| 224 | + continue |
| 225 | + } |
| 226 | + t := count(word) |
| 227 | + if check(counter, t) { |
| 228 | + n = len(word) |
| 229 | + ans = word |
| 230 | + } |
| 231 | + } |
| 232 | + return ans |
| 233 | +} |
96 | 234 | ```
|
97 | 235 |
|
98 | 236 | ### **...**
|
|
0 commit comments