LWC 63:748. Shortest Completing Word
来源:互联网 发布:windows自带录音机 编辑:程序博客网 时间:2024/05/22 07:09
LWC 63:748. Shortest Completing Word
传送门:748. Shortest Completing Word
Problem:
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:
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:
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].
思路:
很暴力,找到一个word符合licensePlate的定义,并不断更新即可。只要licensePlate中出现的字母,word中必须出现,且出现的次数至少相等。采用map计数。
Java版本:
public String shortestCompletingWord(String licensePlate, String[] words) { String ans = ""; int minLen = 1111; int[] map = new int[32]; for (char c : licensePlate.toLowerCase().toCharArray()) if (c >= 'a' && c <= 'z') map[c - 'a'] ++; for (String word : words) { int len = word.length(); if (contains(map, word)) { if (len < minLen) { ans = word; minLen = len; } } } return ans; } boolean contains(int[] map, String word) { int[] map2 = new int[32]; for (char c : word.toLowerCase().toCharArray()) { map2[c - 'a'] ++; } for (int i = 0; i < 32; ++i) { if (map[i] != 0) { if (map2[i] < map[i]) return false; } } return true; }
Python版本:
def shortestCompletingWord(self, licensePlate, words): """ :type licensePlate: str :type words: List[str] :rtype: str """ licence_plate = licensePlate.lower() d = dict() for c in licence_plate: if c.isalpha(): if c not in d: d[c] = 0 d[c] += 1 res = None length = 1111 for word in words: freq = d.copy() for c in word: if c in freq: freq[c] -= 1 if freq[c] == 0: freq.pop(c) if not freq: if len(word) < length: res = word length = len(word) return res
- LWC 63:748. Shortest Completing Word
- 748. Shortest Completing Word
- 748. Shortest Completing Word
- 748. Shortest Completing Word(C++)
- LeetCode | 748. Shortest Completing Word12_17
- LWC 53:691. Stickers to Spell Word
- LWC 57:720. Longest Word in Dictionary
- leetcode--Shortest Word Distance
- Leetcode: Shortest Word Distance
- *LeetCode-Shortest Word Distance
- [leetcode243] Shortest Word Distance
- Shortest Word Distance
- Shortest Word Distance II
- Shortest Word Dsitance III
- [LeetCode243]Shortest Word Distance
- LeetCode Shortest Word Distance
- Leetcode243: Shortest Word Distance
- Leetcode Shortest Word Distance
- opencv常用函数参数说明
- MySQL_5.7免安装版入门(Windows系统)
- 反馈博客bug专用,
- skynet中使用websocket和wss
- Scala学习(五)---Scala中的类
- LWC 63:748. Shortest Completing Word
- Ubuntu下编译opencv各版本
- libsvm学习记录(二)libsvm的使用以及参数意义
- 在决定使用ClickOnce发布你的软件前,应该知道的一些事情(一些常见问题解决方法)
- C语言实现的BP神经网络算法(1)-BPANN原理
- String的endsWith/startsWith
- ubuntukylin32位系统使用libcurl库
- 大规模分布式应用之海量数据和高并发解决方案总结一二
- 如何安装mongodb服务以及如何使用可视化工具连接mongodb数据库