分享

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

 敲代码的小Y 2022-03-31


📢前言

🚀 算法题 🚀
  • 🌲 每天打卡一道算法题,既是一个学习过程,又是一个分享的过程😜
  • 🌲 提示:本专栏解题 编程语言一律使用 C# 和 Java 两种进行解题
  • 🌲 要保持一个每天都在学习的状态,让我们一起努力成为算法大神吧🧐!
  • 🌲 今天是力扣算法题持续打卡第92天🎈!
🚀 算法题 🚀

🌲原题样例:最短补全词

给你一个字符串 licensePlate 和一个字符串数组 words ,请你找出并返回 words 中的 最短补全词 。

补全词 是一个包含 licensePlate 中所有的字母的单词。在所有补全词中,最短的那个就是 最短补全词

在匹配 licensePlate 中的字母时:

  • 忽略 licensePlate 中的 数字和空格 。
  • 不区分大小写。
  • 如果某个字母在 licensePlate 中出现不止一次,那么该字母在补全词中的出现次数应当一致或者更多。

例如:licensePlate = “aBc 12c”,那么它的补全词应当包含字母 'a’、'b’ (忽略大写)和两个 'c’ 。可能的 补全词 有 “abccdef”、“caaacab” 以及 “cbca” 。

请你找出并返回 words 中的 最短补全词

题目数据保证一定存在一个最短补全词。当有多个单词都符合最短补全词的匹配条件时取 words 中 最靠前的 那个。

示例1:

输入: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:

输入:licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
输出:"pest"
解释:licensePlate 只包含字母 "s" 。所有的单词都包含字母 "s" ,其中 "pest""stew"、和 "show" 三者最短。答案是 "pest" ,因为它是三个单词中在 words 里最靠前的那个。

示例 3:

输入:licensePlate = "Ah71752", words = ["suggest","letter","of","husband","easy","education","drug","prevent","writer","old"]
输出:"husband"

示例4:

输入:licensePlate = "OgEu755", words = ["enough","these","play","wide","wonder","box","arrive","money","tax","thus"]
输出:"enough"

提示:

  • 1 <= licensePlate.length <= 7
  • licensePlate 由数字、大小写字母或空格 ’ ’ 组成
  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 15
  • words[i] 由小写英文字母组成

🌻C#方法:统计字符出现次数

根据题意,我们先统计licensePlate 中每个字母的出现次数(忽略大小写)

然后遍历 words 中的每个单词,

若 26 个字母在该单词中的出现次数均不小于在licensePlate 中的出现次数,则该单词是一个补全词。返回最短且最靠前的补全词。

代码:

public class Solution {
    public string ShortestCompletingWord(string licensePlate, string[] words) {
        int[] cnt = new int[26];
        foreach (char ch in licensePlate) {
            if (char.IsLetter(ch)) {
                ++cnt[char.ToLower(ch) - 'a'];
            }
        }
        int idx = -1;
        for (int i = 0; i < words.Length; ++i) {
            int[] c = new int[26];
            foreach (char ch in words[i]) {
                ++c[ch - 'a'];
            }
            bool ok = true;
            for (int j = 0; j < 26; ++j) {
                if (c[j] < cnt[j]) {
                    ok = false;
                    break;
                }
            }
            if (ok && (idx < 0 || words[i].Length < words[idx].Length)) {
                idx = i;
            }
        }
        return words[idx];
    }
}

执行结果

通过
执行用时:116 ms,在所有 C# 提交中击败了62.50%的用户
内存消耗:43.4 MB,在所有 C# 提交中击败了37.90%的用户

🌻Java 方法:统计字符出现次数

思路解析
该方法和上述C#思路一致,不过代码不同!

代码:

class Solution {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] cnt = new int[26];
        for (int i = 0; i < licensePlate.length(); ++i) {
            char ch = licensePlate.charAt(i);
            if (Character.isLetter(ch)) {
                ++cnt[Character.toLowerCase(ch) - 'a'];
            }
        }
        int idx = -1;
        for (int i = 0; i < words.length; ++i) {
            int[] c = new int[26];
            for (int j = 0; j < words[i].length(); ++j) {
                char ch = words[i].charAt(j);
                ++c[ch - 'a'];
            }
            boolean ok = true;
            for (int j = 0; j < 26; ++j) {
                if (c[j] < cnt[j]) {
                    ok = false;
                    break;
                }
            }
            if (ok && (idx < 0 || words[i].length() < words[idx].length())) {
                idx = i;
            }
        }
        return words[idx];
    }
}

执行结果

通过
执行用时:3 ms,在所有 Java  提交中击败了95.41%的用户
内存消耗:38.8 MB,在所有 Java 提交中击败了46.40%的用户

复杂度分析

时间复杂度:O( N+L+M·|Σ| ),其中 N 是字符串 licensePlate 的长度,L 是 words 中的所有字符串的长度之和,M 是 words 数组的长度,∣Σ∣ 为字符集合的大小,本题中有 26 个英文字母,即 ∣Σ∣=26
空间复杂度:O(|Σ|) 

💬总结

  • 今天是力扣算法题打卡的第九十二天!
  • 文章采用 C#Java 两种编程语言进行解题
  • 一些方法也是参考力扣大神写的,也是边学习边分享,再次感谢算法大佬们
  • 那今天的算法题分享到此结束啦,明天再见!
    请添加图片描述

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多