748.最短补全词
链接:748.最短补全词
难度:Easy
标签:数组、哈希表、字符串
简介:给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。
题解 1 - typescript
- 编辑时间:2021-12-10
- 执行用时:88ms
- 内存消耗:41.9MB
- 编程语言:typescript
- 解法介绍:哈希。
function shortestCompletingWord(licensePlate: string, words: string[]): string {
const reg_lowchar = /[a-z]/;
const map: Record<string, number> = {};
for (const ch of licensePlate.toLowerCase()) {
if (reg_lowchar.test(ch)) map[ch] = (map[ch] ?? 0) + 1;
}
return words
.filter(word => {
const wmap = { ...map };
for (const ch of word) {
if (wmap[ch]) wmap[ch]--;
}
return Object.values(wmap).every(v => v <= 0);
})
.sort((a, b) => a.length - b.length)[0];
}