首页 技术 正文
技术 2022年11月6日
0 收藏 317 点赞 797 浏览 1216 个字

748. 最短完整词

如果单词列表(words)中的一个单词包含牌照(licensePlate)中所有的字母,那么我们称之为完整词。在所有完整词中,最短的单词我们称之为最短完整词。

单词在匹配牌照中的字母时不区分大小写,比如牌照中的 “P” 依然可以匹配单词中的 “p” 字母。

我们保证一定存在一个最短完整词。当有多个单词都符合最短完整词的匹配条件时取单词列表中最靠前的一个。

牌照中可能包含多个相同的字符,比如说:对于牌照 “PP”,单词 “pair” 无法匹配,但是 “supper” 可以匹配。

示例 1:输入:licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
输出:"steps"
说明:最短完整词应该包括 "s"、"p"、"s" 以及 "t"。对于 "step" 它只包含一个 "s" 所以它不符合条件。同时在匹配过程中我们忽略牌照中的大小写。示例 2:输入:licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
输出:"pest"
说明:存在 3 个包含字母 "s" 且有着最短长度的完整词,但我们返回最先出现的完整词。

注意:

牌照(licensePlate)的长度在区域[1, 7]中。

牌照(licensePlate)将会包含数字、空格、或者字母(大写和小写)。

单词列表(words)长度在区间 [10, 1000] 中。

每一个单词 words[i] 都是小写,并且长度在区间 [1, 15] 中。

class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] license = new int[26];
for(char c : licensePlate.toCharArray()){
if(c >= 'a' && c <= 'z')
license[c - 'a']++;
else if(c >= 'A' && c <= 'Z')
license[c - 'A']++;
}
String res = null;
for(String word : words){
if(isContains(license,word))
if(res == null || word.length() < res.length())
res = word;
} return res;
} private boolean isContains(int[] license,String word){
int[] ans = new int[26];
for(char c : word.toCharArray()){
if(c >= 'a' && c <= 'z')
ans[c - 'a']++;
else if(c >= 'A' && c <= 'Z')
ans[c - 'A']++;
} for(int i = 0;i < 26;i++)
if(ans[i] < license[i])
return false;
return true;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905