748. Shortest Completing Word

748. Shortest Completing Word

题目描述

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate

Here, for letters we ignore case. For example, “P” on the licensePlate still matches “p” on the word.

It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.

The license plate might have the same letter occurring multiple times. For example, given a licensePlate of “PP”, the word “pair” does not complete the licensePlate, but the word “supper” does.

Example 1:

1
2
3
4
5
Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
Output: "steps"
Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
Note that the answer is not "step", because the letter "s" must occur in the word twice.
Also note that we ignored case for the purposes of comparing whether a letter exists in the word.

Example 2:

1
2
3
4
Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
Output: "pest"
Explanation: There are 3 smallest length words that contains the letters "s".
We return the one that occurred first.

Note:
licensePlate will be a string with length in range [1, 7].
licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
words will have a length in the range [10, 1000].
Every words[i] will consist of lowercase letters, and have length in range [1, 15].

题目大意

给定一个车牌licensePlate,在words中寻找包含了车牌上所有字符的元素。返回最短的匹配元素,如果有多个,则返回第一个。

解题思路

暴力搜索,辅助哈希表。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
func shortestCompletingWord(_ licensePlate: String, _ words: [String]) -> String {
var dict = [Character: Int]()
var ans = ""
let express = try! NSRegularExpression(pattern: "[a-z]", options: .caseInsensitive)
for n in licensePlate.lowercased() {
let s = String(n)
//正则匹配字母
if express.matches(in: s, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSRange(location: 0, length: 1))
.count >= 1{
dict[n] = (dict[n] ?? 0) + 1
}
}
for word in words {
var tmp = dict
for char in word {
if let count = tmp[char] {
tmp[char] = count - 1
if count <= 1 {
tmp.removeValue(forKey: char)
}
}
}
if tmp.isEmpty {
if ans.isEmpty { ans = word }
//判断最小匹配元素
ans = ans.count <= word.count ? ans : word
}
}
return ans
}
}
0%