刷题记录-20211210
今天这个简单题,没啥好说的,把代码贴这class Solution {public String shortestCompletingWord(String licensePlate, String[] words) {int len = licensePlate.length();//题目给出的条件,最长单词就是15个字母int shortestLen = 15;String resStr =
·
今天这个简单题,没啥好说的,把代码贴这
class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int len = licensePlate.length();
//题目给出的条件,最长单词就是15个字母
int shortestLen = 15;
String resStr = "";
for(String word : words)
{
if(cover(licensePlate,word))
{
if(word.length() < shortestLen)
{
resStr = word;
shortestLen = word.length();
}
}
}
return resStr;
}
public boolean cover(String licensePlate,String word)
{
int[] charInfo = new int[26];
for(int i =0;i < licensePlate.length();i++)
{
char letter = licensePlate.charAt(i);
if(letter - 'a' >= 0 && letter - 'a' < 26)
{
charInfo[letter-'a']++;
}
if(letter - 'A' >= 0 && letter - 'A' < 26)
{
charInfo[letter-'A']++;
}
}
for(int i =0;i < word.length();i++)
{
char letter = word.charAt(i);
charInfo[letter-'a']--;
}
for(int i = 0;i < 26;i++)
{
if(charInfo[i] > 0)
{
return false;
}
}
return true;
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)