From 0ba50a14b62fabfac8c7dd3ba42c79d8cd8afad9 Mon Sep 17 00:00:00 2001 From: FoundAdd <2738023398@qq.com> Date: Fri, 10 Dec 2021 19:51:09 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC748=E9=A2=98=EF=BC=8C=E6=B2=A1?= =?UTF-8?q?=E5=81=9A=E5=AE=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 6 ++++++ src/main/java/cn/hy/leetcode/Solution.java | 22 ++++++++++++++++++++++ src/main/java/cn/hy/test/SolutionTest.java | 14 ++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/cn/hy/leetcode/Solution.java b/src/main/java/cn/hy/leetcode/Solution.java index c21b96f..d8e3f19 100644 --- a/src/main/java/cn/hy/leetcode/Solution.java +++ b/src/main/java/cn/hy/leetcode/Solution.java @@ -3,6 +3,7 @@ package cn.hy.leetcode; import cn.hy.util.StringUtil; import java.util.*; +import java.util.stream.Collectors; /** * LeetCode算法题解法 @@ -98,6 +99,7 @@ public class Solution { } return resultList.toArray(result); } + /** * 541. 反转字符串 * 给定一个字符串 s 和一个整数 k,从字符串开头算起,每 2k 个字符反转前 k 个字符。 @@ -121,6 +123,26 @@ public class Solution { return result; } + /** + * 748.最短补全词 + * 给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。 + * 补全词 是一个包含 licensePlate 中所有的字母的单词。在所有补全词中,最短的那个就是 最短补全词 。 + * @param licensePlate 匹配字符串 + * @param words 待匹配的字符串数组 + * @return 匹配结果 + */ + public String shortestCompletingWord(String licensePlate, String[] words) { + List template = Arrays.asList(licensePlate.toLowerCase().replaceAll("[^a-z]", "").split("")); + List collect = Arrays.stream(words).map( + word -> { + Arrays.stream(word.split("")).filter(letter -> template.contains(letter)).collect(Collectors.toList()); + return word; + } + ).collect(Collectors.toList()); + + return collect.stream().sorted(Comparator.comparingInt(String::length)).findFirst().get(); + } + /** * 878. 第N个神奇数字 * 如果正整数可以被 A 或 B 整除,那么它是神奇的。 diff --git a/src/main/java/cn/hy/test/SolutionTest.java b/src/main/java/cn/hy/test/SolutionTest.java index c91032c..59d44eb 100644 --- a/src/main/java/cn/hy/test/SolutionTest.java +++ b/src/main/java/cn/hy/test/SolutionTest.java @@ -49,6 +49,7 @@ public class SolutionTest { System.out.println(res); } } + /** * 541.反转字符串Ⅱ */ @@ -56,6 +57,18 @@ public class SolutionTest { public void testReverseStr2(){ } + + /** + * 748.最短补全词 + */ + @Test + public void testShortestCompletingWord() { + String licensePlate = "1s3 PSt"; + String[] words = {"step", "steps", "stripe", "stepple"}; + + System.out.println(solution.shortestCompletingWord(licensePlate, words)); + } + /** * 878.第N个神奇数字 */ @@ -63,6 +76,7 @@ public class SolutionTest { public void testNthMagicalNumber(){ } + /** * 1234.替换子串得到平衡字符串 */ -- Gitee