LeetCode:748.最短完整词

题目:

如果单词列表(words)中的一个单词包含牌照(licensePlate)中所有的字母,那么我们称之为完整词。在所有完整词中,最短的单词我们称之为最短完整词。

单词在匹配牌照中的字母时不区分大小写,比如牌照中的 "P" 依然可以匹配单词中的 "p" 字母。

我们保证一定存在一个最短完整词。当有多个单词都符合最短完整词的匹配条件时取单词列表中最靠前的一个。

牌照中可能包含多个相同的字符,比如说:对于牌照 "PP",单词 "pair" 无法匹配,但是 "supper" 可以匹配。


示例 1:

输入:licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
输出:"steps"
说明:最短完整词应该包括 "s""p""s" 以及 "t"。对于 "step" 它只包含一个 "s" 所以它不符合条件。同时在匹配过程中我们忽略牌照中的大小写。
 

示例 2:

输入:licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
输出:"pest"
说明:存在 3 个包含字母 "s" 且有着最短长度的完整词,但我们返回最先出现的完整词。

源码:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
class Solution {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        Map<Character, Integer> map1 = new HashMap<>();
        Map<Character, Integer> map2 = new HashMap<>();
        char[] chars = licensePlate.toCharArray();
        // 将 licensePlate 中的字母转成小写字母然后加入 map1 之中
        for (int i = 0; i < chars.length; i++) {
            // 将大写字母转换成小写字母
            if (chars[i] >= 'A' && chars[i] <= 'Z') {
                chars[i] = (char)(chars[i] + 32);
            }
            if (chars[i] >= 'a' && chars[i] <= 'z') {
                map1.put(chars[i],map1.getOrDefault(chars[i],0)+1);
            }
        }
        // res 是用来保存最后的结果
        String res = null;
        for (int j = 0; j < words.length; j++) {
            // 进入 words 开始进行筛选和比较
            String str = words[j];
            // 特别注意此处,map2 是深拷贝 map1
            // 目的是为了筛选 words 中每个单词是否符合条件
            map2 = deepClone(map1);
            char[] word = str.toCharArray();
            for (int k = 0; k < word.length; k++) {
                if (map2.containsKey(word[k])) {
                    // 如果包含有 map1 中的某个字母
                    // 就对该字母进行 -1 操作
                    map2.put(word[k], map2.get(word[k]) - 1);
                    // 如果某个键值为 0 则删除该键值对
                    // 以便后面 map2.size() == 0 判断
                    if (map2.get(word[k]) == 0) {
                        map2.remove(word[k]);
                    }
                }
            }
            // 如果最后循环结束,map2 为 0 说明该单词满足条件
            if (map2.size() == 0) {
                if (res == null) {
                    res = str;
                } else {
                    // 如果满足条件的单词长度比原先 res 的单词长度小
                    // 那么 res 就为该单词
                    res = res.length() > str.length() ? str : res;
                }
            }
        }
        return res;
    }
    // 深拷贝的函数
    public static <T> Map deepClone(Map obj){
        T clonedObj = null;
        try {
            ByteArrayOutputStream baos = 
                    new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            oos.close();

            ByteArrayInputStream bais = 
                    new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            clonedObj = (T) ois.readObject();
            ois.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return (Map) clonedObj;
    }
}
发布了340 篇原创文章 · 获赞 2 · 访问量 8297

猜你喜欢

转载自blog.csdn.net/qq_45239139/article/details/104050340