package leetcode.day_12_10;

import org.junit.Test;

/**
* 给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。
* <p>
* 补全词 是一个包含 licensePlate 中所有的字母的单词。在所有补全词中,最短的那个就是 最短补全词 。
* <p>
* 在匹配 licensePlate 中的字母时:
* <p>
* 忽略licensePlate 中的 数字和空格 。
* 不区分大小写。
* 如果某个字母在 licensePlate 中出现不止一次,那么该字母在补全词中的出现次数应当一致或者更多。
* 例如:licensePlate = "aBc 12c",那么它的补全词应当包含字母 'a'、'b' (忽略大写)和两个 'c' 。可能的 补全词 有 "abccdef"、"caaacab" 以及 "cbca" 。
* <p>
* 请你找出并返回 words 中的 最短补全词 。题目数据保证一定存在一个最短补全词。当有多个单词都符合最短补全词的匹配条件时取 words 中 最靠前的 那个。
* <p>
* 示例 1:
* <p>
* 输入:licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
* 输出:"steps"
* 解释:最短补全词应该包括 "s"、"p"、"s"(忽略大小写) 以及 "t"。
* "step" 包含 "t"、"p",但只包含一个 "s",所以它不符合条件。
* "steps" 包含 "t"、"p" 和两个 "s"。
* "stripe" 缺一个 "s"。
* "stepple" 缺一个 "s"。
* 因此,"steps" 是唯一一个包含所有字母的单词,也是本例的答案。
* 示例 2:
* <p>
* 输入:licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
* 输出:"pest"
* 解释:licensePlate 只包含字母 "s" 。所有的单词都包含字母 "s" ,其中 "pest"、"stew"、和 "show" 三者最短。答案是 "pest" ,因为它是三个单词中在 words 里最靠前的那个。
* 示例 3:
* <p>
* 输入:licensePlate = "Ah71752", words = ["suggest","letter","of","husband","easy","education","drug","prevent","writer","old"]
* 输出:"husband"
* 示例 4:
* <p>
* 输入:licensePlate = "OgEu755", words = ["enough","these","play","wide","wonder","box","arrive","money","tax","thus"]
* 输出:"enough"
* 示例 5:
* <p>
* 输入:licensePlate = "iMSlpe4", words = ["claim","consumer","student","camera","public","never","wonder","simple","thought","use"]
* 输出:"simple"
*
* @author soberw
* @Classname ShortestCompletingWord748
* @Description
* @Date 2021-12-10 11:47
*/
public class ShortestCompletingWord0748 {
//思路,通过正则匹配筛出所有的字母,遍历字母表(从前向后,保证最后结果在最前),找到符合条件的且最短的,输出(不成立)
//创建一个26单位长度的数组,记录出现次数 /**
* @param licensePlate 待匹配单词
* @param words 单词表
* @description: 匹配最短补全词
* @return: 最短补全词
* @author: soberw
* @time: 2021/12/10 11:51
*/
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] count = new int[26];
for (int i = 0; i < licensePlate.length(); i++) {
if (Character.isLetter(licensePlate.charAt(i))) {
count[Character.toLowerCase(licensePlate.charAt(i)) - 'a']++;
}
} String str = null;
for (String word : words) {
if (infor(word, count) && (str == null || str.length() > word.length())) {
str = word;
}
}
return str;
} private boolean infor(String word, int[] lcnt) {
int[] count = new int[26];
for (int i = 0; i < word.length(); i++) {
count[word.charAt(i) - 'a']++;
} for (int i = 0; i < 26; i++) {
if (lcnt[i] > count[i]) {
return false;
}
} return true;
} @Test
public void test() {
ShortestCompletingWord0748 s = new ShortestCompletingWord0748();
System.out.println(s.shortestCompletingWord("1s3 456", new String[]{"looks", "pest", "stew", "show"}));
//"1s3 456"
//["looks","pest","stew","show"]
}
}

LeetCode随缘刷题之最短补全词的更多相关文章

  1. LeetCode随缘刷题之最长回文子串

    这一题我用的相对比较笨的方法. 相对于大佬们用的动态规划法,比较复杂.但却更容易理解,我主要是通过记录下标来确定最长回文串的. package leetcode.day_12_06; /** * 给你 ...

  2. Leetcode随缘刷题之寻找两个正序数组的中位数

    我一上来没读清题,想着这题这么简单,直接就上手写了: package leetcode.day_12_05; import java.util.ArrayList; import java.util. ...

  3. LeetCode随缘刷题之Java经典面试题将一个字符串数组进行分组输出,每组中的字符串都由相同的字符组成

    今天给大家分享一个Java经典的面试题,题目是这样的: 本题是LeetCode题库中的49题. 将一个字符串数组进行分组输出,每组中的字符串都由相同的字符组成 举个例子:输入["eat&qu ...

  4. LeetCode随缘刷题之转化成小写字母

    这道题应该是最简单的一道题了把,简直在侮辱我. package leetcode.day_12_12; /** * 709. 转换成小写字母 * 给你一个字符串 s ,将该字符串中的大写字母转换成相同 ...

  5. LeetCode随缘刷题之截断句子

    这道题相对比较简单.正好最近学到StringBuilder就用了. package leetcode.day_12_06; /** * 句子 是一个单词列表,列表中的单词之间用单个空格隔开,且不存在前 ...

  6. LeetCode随缘刷题之无重复字符的最长子串

    欢迎评论区交流. package leetcode.day_12_04; /** * 给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度. * <p> * 示例1: * &l ...

  7. LeetCode随缘刷题之赎金信

    欢迎评论区讨论. package leetcode.day_12_04; /** * 为了不在赎金信中暴露字迹,从杂志上搜索各个需要的字母,组成单词来表达意思. * * 给你一个赎金信 (ransom ...

  8. LeetCode随缘刷题之两数相加

    逐步解释,有说错的地方欢迎指正. package leetcode.day_12_03; /** * 给你两个非空 的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存 ...

  9. LeetCode随缘刷题之盛最多水的容器

    package leetcode.day_01_30; /** * 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) .在坐标内画 n 条垂直线,垂直线 i的两个端 ...

随机推荐

  1. 过年有燃放烟花爆竹禁令那我们用css写一个仙女棒烟花看看吧

    先是去找了一张简易画的烟花照片,可以看出主要结构为歪曲的线条结构. 方案一: 弯曲的线条第一反应到的就是"圆角边框": width: 200px; height: 200px; b ...

  2. python的作用域、globals()-全局变量 和 locals()-局部变量

    在python中,函数会创建一个自己的作用域,也称为为命名空间.当我们在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 我们自己定义的全局变量均在python内建的globals()函数中 ...

  3. react子组件向父组件传值

    子组件向父组件传值,注意父组件传递函数的时候必须绑定this到当前父组件(handleEmail={this.handleEmail.bind(this)}),不然会报错 /***实现在输入框输入邮箱 ...

  4. java mapreduce实现网站PV分析

    原文链接: https://www.toutiao.com/i6765677128022229517/ PV 是Page Views的缩写,即页面浏览量,用户每一次对网站中的每个网页访问均被记录一次. ...

  5. 51 Nod 1006 最长公共子序列(LCS & DP)

    原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 题目分析: 首先先知道LCS问题,这有两种: Long ...

  6. mysql之突破secure_file_priv写webshell

    在某些情况下,当我们进入了一个网站的phpMyAdmin时,想通过select into outfile来写shell,但是通常都会报错. 这是因为在mysql 5.6.34版本以后 secure_f ...

  7. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  8. springMvc 启动过程

    转载自https://www.jianshu.com/p/dc64d02e49ac 这里给出一个简洁的文字描述版SpringMVC启动过程: tomcat web容器启动时会去读取web.xml这样的 ...

  9. MySQL的MyISAM与InnoDB的索引方式

    在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,本文主要讨论MyISAM和InnoDB两个存储引擎的索引实现方式. MyISAM索引实现 MyISAM引擎使用B+Tr ...

  10. jmeter - 阶梯式性能指标监听

    概述 我们在进行阶梯式压力测试的时候,聚合报告生成的结果是一个汇总数据.并不会阶梯式的统计压测性能数据.这样我们就不能去对比不同阶梯压力下的性能数据变化趋势. 期望 假设现在一共会加载100个线程,我 ...