leetCode最短补全词

题目要求如下:
给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。
补全词 是一个包含 licensePlate 中所有字母的单词。 在匹配 licensePlate 中的字母时: 忽略 licensePlate 中的 数字和空格 。 不区分大小写。 如果某个字母在 licensePlate
中出现不止一次,那么该字母在补全词中的出现次数应当一致或者更多。 例如:licensePlate = “aBc12c”,那么它的补全词应当包含字母 ‘a’、‘b’ (忽略大写)和两个 ‘c’ 。可能的 补全词 有 “abccdef”、“caaacab” 以及 “cbca” 。

请你找出并返回 words 中的 最短补全词 。题目数据保证一定存在一个最短补全词。当有多个单词都符合最短补全词的匹配条件时取 words中 第一个 出现的那个。

思路:
1.去掉licensePlate中的数字和空格,转换大小写
2.刷选符合要求的字符串
3.查找符合要求的字符串中最短字符串
4.若最短字符串存在多个,则取words数组中最前面的一个

实现的时候碰到了一个问题,我一直想的是在循环中去判断某个字符串蕴含多个相同字符的问题,实现的时候把自己搞糊涂了

public class Test {public static void main(String[] args) {String licensePlate = "1s3 456";String[] words = new String[]{"looks", "pest", "stew", "show", "shows"};Soletion soletion = new Soletion();System.out.println(soletion.shortestComletingWord(licensePlate, words));}
}class Soletion {public String shortestComletingWord(String licensePlate, String[] words) {// 去掉licensePlate中的数字和空格,转换大小写// 刷选符合要求的字符串// 查找符合要求的字符串中最短字符串// 若最短字符串存在多个,则去words数组中最前面的一个
List<String> list = new ArrayList<>();
//去掉字符串内的所有空格&&将字符串大小写统一&&去掉字符串中的数字String str = licensePlate.toLowerCase().replaceAll(" ", "").replaceAll("\\d+", "");System.out.println(str);char[] arr = str.toCharArray();System.out.println(Arrays.toString(arr));
//循环找出符合要求的字符串,并添加到容器内for (int i = 0; i < words.length; i++) {for (int j = 0; j < arr.length; j++) {if (words[i].contains(arr[j] + "")) {for (int k = 0; k < arr.length; k++) {if (arr[k] == arr[j] && k != j) {if (words[i].indexOf(arr[j]) != words[i].lastIndexOf(arr[j]) && j == arr.length - 1) {System.out.println(4);list.add(words[i]);} else {System.out.println(3);break;}}if (arr[k] == arr[j] && k == j) {System.out.println(2);}}} else {System.out.println(1);break;}}}System.out.println(Arrays.toString(list.toArray()));String[] strings = new String[list.size()];for (int i = 0; i < list.size(); i++) {strings[i] = list.get(i);}
//取数组中最短字符串的下标地址int index = 0;for (int i = 0; i < strings.length; i++) {if (strings[i].length() <= strings[index].length()) {index = i;}}
//比较最短字符串下标地址相同但是在最前面的for (int i = 0; i < strings.length; i++) {if (strings[i].length() == strings[index].length()) {if (strings[i] != strings[index]) {index = i;break;}}}return strings[index];

后面征询了他人的意见后,我决定用map来处理单个字符出现多次的问题
同时在写的时候我发现最开始的代码写的实在是太耦合了,而且复杂,我决定将做同一件事情的动作抽出来写个方法调用

    //写一个方法统计字符串某个字符出现的次数public int select(String str, char a) {int count = 0;int oldLength = str.length();str = str.replaceAll(String.valueOf(a), "");int newLength = str.length();count = oldLength - newLength;return count;

这里的时候我想用map的目的不也是取字符串中某个字符出现的次数么,那么也就可以这样:

    public static void main(String[] args) {String licensePlate = "1s3 456";String[] words = new String[]{"looks", "pest", "stew", "show","shows"};Soletion soletion = new Soletion();System.out.println(soletion.shortestComletingWord(licensePlate, words));}
}class Soletion {public String shortestComletingWord(String licensePlate, String[] words) {// 去掉licensePlate中的数字和空格,转换大小写// 刷选符合要求的字符串// 查找符合要求的字符串中最短字符串// 若最短字符串存在多个,则去words数组中最前面的一个
List<String> list = new ArrayList<>();
//去掉字符串内的所有空格&&将字符串大小写统一&&去掉字符串中的数字String str = licensePlate.toLowerCase().replaceAll("\\s+", "").replaceAll("\\d+", "");int licenLength = str.length();//方便循环的时候作为参数//循环找出符合要求的字符串,并添加到容器内for (int i = 0; i < words.length; i++) {//判断字符串数组每个的长度和需要比较的字符个数,优化性能if (words[i].length() < str.length()) {continue;}
//这里我再次剪掉了一个数组,用了String的charAt,提升内存的利用for (int j = 0; j < licenLength; j++) {if (words[i].contains(str.charAt(j) + "")) {if (select(str, str.charAt(j)) != 1) {if (select(words[i], str.charAt(j)) != select(str, str.charAt(j))) {break;}}if (j == licenLength - 1) {list.add(words[i]);break;}} else {break;}}}//将接收到符合要求的字符串放到字符串数组方便操作String[] strings = list.toArray(new String[0]);//取数组中最短字符串的下标地址int index = 0;for (int i = 0; i < strings.length; i++) {if (strings[i].length() <= strings[index].length()) {index = i;}}
//比较最短字符串下标地址相同但是在最前面的for (int i = 0; i < strings.length; i++) {if (strings[index].length() == strings[i].length()) {index = i;break;}}return strings[index];}//写一个方法统计字符串某个字符出现的次数public int select(String str, char a) {int count = 0;int oldLength = str.length();str = str.replaceAll(String.valueOf(a), "");int newLength = str.length();count = oldLength - newLength;return count;}
}

代码如下:

public class Test {public static void main(String[] args) {String licensePlate = "1s3 456";String[] words = new String[]{"looks", "pest", "stew", "show", "shows"};Soletion soletion = new Soletion();System.out.println(soletion.shortestComletingWord(licensePlate, words));}
}class Soletion {public String shortestComletingWord(String licensePlate, String[] words) {// 去掉licensePlate中的数字和空格,转换大小写// 刷选符合要求的字符串// 查找符合要求的字符串中最短字符串// 若最短字符串存在多个,则去words数组中最前面的一个List<String> list = new ArrayList<>();
//去掉字符串内的所有空格&&将字符串大小写统一&&去掉字符串中的数字String str = licensePlate.toLowerCase().replaceAll("\\s+", "").replaceAll("\\d+", "");int licenLength = str.length();//方便循环的时候作为参数//循环找出符合要求的字符串,并添加到容器内for (int i = 0; i < words.length; i++) {//判断字符串数组每个的长度和需要比较的字符个数,优化性能if (words[i].length() < str.length()) {continue;}
//这里我再次剪掉了一个数组,用了String的charAt,提升内存的利用for (int j = 0; j < licenLength; j++) {if (words[i].contains(str.charAt(j) + "")) {if (select(str, str.charAt(j)) != 1) {if (select(words[i], str.charAt(j)) != select(str, str.charAt(j))) {break;}}if (j == licenLength - 1) {list.add(words[i]);break;}} else {break;}}}//将接收到符合要求的字符串放到字符串数组方便操作String[] strings = list.toArray(new String[0]);//取数组中最短字符串的下标地址int index = 0;for (int i = 0; i < strings.length; i++) {if (strings[i].length() <= strings[index].length()) {index = i;}}
//比较最短字符串下标地址相同但是在最前面的for (int i = 0; i < strings.length; i++) {if (strings[index].length() == strings[i].length()) {index = i;break;}}return strings[index];}//写一个方法统计字符串某个字符出现的次数
//        public int select(String str, char a) {//            int count = 0;
//            int oldLength = str.length();
//            str = str.replaceAll(String.valueOf(a), "");
//            int newLength = str.length();
//            count = oldLength - newLength;
//            return count;public int select (String str,char a){int count = 0;char[] chars = str.toCharArray();for (char i : chars) {if (i == a) {count++;}}return count;}}

测试的时候发现在执行时间和内存消耗上比较大,改了一下把执行时间提升了一倍,内存消耗还是依旧。

        //写一个方法统计字符串某个字符出现的次数
//        public int select(String str, char a) {//            int count = 0;
//            int oldLength = str.length();
//            str = str.replaceAll(String.valueOf(a), "");
//            int newLength = str.length();
//            count = oldLength - newLength;
//            return count;public int select (String str,char a){int count = 0;char[] chars = str.toCharArray();for (char i : chars) {if (i == a) {count++;}}return count;}

在题解的讨论区,看了别人的解法,发现真的是人才辈出!

public class Test {public static void main(String[] args) {String licensePlate = "1s3 456";String[] words = new String[]{"looks", "pest", "stew", "show", "shows"};Soletion soletion = new Soletion();System.out.println(soletion.shortestComletingWord(licensePlate, words));}
}class Soletion {public String shortestComletingWord(String licensePlate, String[] words) {int n = licensePlate.length(),m = words.length;licensePlate = licensePlate.toLowerCase();//arr数组内存放的是字符的出现的次数int[] arr = new int[26];for (int i = 0; i < n; i++) {char c = licensePlate.charAt(i);if (c >= 'a' && c <= 'z'){arr[c - 'a'] ++;}}// for (int num : arr) {//     System.out.println(num);// }int minLength = 16;String ans = "";for (int i = 0; i < m; i++) {String s = words[i];if (s.length() < minLength) {int[] arr2 = Arrays.copyOf(arr,26);if (check(arr2,s)){minLength = s.length();ans = s;}}}return ans;}public boolean check(int[] arr, String word) {int n = word.length();for (int i = 0; i < n; i++) {arr[word.charAt(i) - 'a']--;}for (int num : arr) {if (num > 0) {return false;}}return true;

最后,如有疑问请留言

leetCode最短补全词相关推荐

  1. Leetcode 748 最短补全词

    题目 给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 . 补全词 是一个包含 licensePlate 中所有的字母的单词.在所 ...

  2. 【解题报告】Leecode 748. 最短补全词——Leecode每日一题系列

    题目链接:https://leetcode-cn.com/problems/shortest-completing-word/ 题解汇总:https://leetcode-cn.com/problem ...

  3. 748. 最短补全词( Shortest Completing Word) 3ms做法心得

    做法大致就是将每个字符对应的ASCII码-'a'后存进数组,然后对照两个数组来求最优解. import java.util.Locale;//748. 最短补全词 public class Short ...

  4. 【算法千题案例】每日LeetCode打卡——92.最短补全词

  5. 字符串匹配数据结构 --Trie树 高效实现搜索词提示 / IDE自动补全

    文章目录 1. 算法背景 2. Trie 树实现原理 2.1 Trie 树的构建 2.2 Trie树的查找 2.3 Trie树的遍历 2.4 Trie树的时间/空间复杂度 2.5 Trie 树 Vs ...

  6. LeetCode 642. 设计搜索自动补全系统(Trie树)

    文章目录 1. 题目 2. 解题 1. 题目 为搜索引擎设计一个搜索自动补全系统. 用户会输入一条语句(最少包含一个字母,以特殊字符 '#' 结尾). 除 '#' 以外用户输入的每个字符,返回历史中热 ...

  7. 【工具】Vscode翻译插件推荐(不用谷歌翻译api、支持短句英汉互译、支持查词、支持自动补全、不需要浏览器)

    需求: 1)偶尔需要查英文生词: 2)有时候想不起来中文对应的英文: 3)不想回到浏览器打开一堆网页: 4)谷歌翻译挂了. 偶尔需要的需求: 1)短句翻译. 因为谷歌翻译挂了,首先,排除最热门的翻译插 ...

  8. spring-data-elasticsearch suggest实现错词纠正和自动补全

    背景: 项目中使用了spring-data-elasticsearch注解配置了mapping,实现全文搜索功能. 接到需求需要实现错词纠正功能和自动补全功能: 在网上找的资料,可以通过ES的sugg ...

  9. ES 7.X 做类百度搜索,进行搜索自动补全和热搜词及拼音功能实现

    文章目录 前言 一.如何使用ES做类似百度的检索? 二.全文检索自动补齐 1.创建索引 2.添加数据 3.高级检索 三 热搜词 1.思路 2.DSL语句 3.java代码实现 四 拼音补全 1.DSL ...

最新文章

  1. 终极大招——怎么在学术会议上有所收获?
  2. 计算机技术与软件专业技术资格(水平)考试常见问题
  3. xmake新增对WDK驱动编译环境支持
  4. java filter注入,Spring-boot中Filter注入bean
  5. 一步步编写操作系统 41 快表tlb 简介
  6. 从工具的奴隶到工具的主人
  7. 在STM32单片机上跑神经网络算法
  8. delphi7 如何判定dbgrid两行重复_教你如何在服装上加入好看的毛线刺绣花边
  9. linux远程连接telnet命令,Linux中的ssh,ping,ftp,telnet远程登录及通信相关的命令...
  10. 序列化和反序列化(二)——Serializable 接口
  11. IT行业必须知道的基础知识
  12. 软件项目管理(SPM)课程总结及感想
  13. 5款精美APP原型设计模板,触手可得
  14. 3.9、互斥锁(互斥量)
  15. 三大电信公司同领TDS牌照可能性最大
  16. 共享文件夹—— 一个实现Mac与PC互传文件,维护同一个文件夹简单的方法
  17. Python|图片转二维码
  18. 《神经科学:探索脑》学习笔记(第13章 运动的脊髓控制)
  19. 币须知道 |首批19家企业入驻杭州区块链产业园,纳斯达克:股市中仅5%的 IT供应商部署区块链...
  20. element dialog 设置背景色

热门文章

  1. 接力队选拔matlab,这是我见过的操作最好的接力队之一= =
  2. 2021正睿csp7连day2
  3. RFID固定资产管理系统能给企业带来什么?
  4. 微软云中国的服务器在哪,微软云计算平台Windows Azure将落户中国
  5. 如何隐藏、显示Excel工作表中的公式
  6. Java POI实现pptpptx转换为pdf文件
  7. 义隆单片机学习笔记之(四) 编程及烧录
  8. Android打包apk实现原理与流程(雷惊风)
  9. 紫光公有云能后发先至吗?
  10. qq邮箱服务器密码,QQ邮箱授权码如何获取?