LWC 63:748. Shortest Completing Word

LWC 63:748. Shortest Completing Word

传送门:748. Shortest Completing Word

Problem:

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate Here, for letters we ignore case. For example, “P” on the licensePlate still matches “p” on the word. It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array. The license plate might have the same letter occurring multiple times. For example, given a licensePlate of “PP”, the word “pair” does not complete the licensePlate, but the word “supper” does.

Example 1:

Input: licensePlate = “1s3 PSt”, words = [“step”, “steps”, “stripe”, “stepple”] Output: “steps” Explanation: The smallest length word that contains the letters “S”, “P”, “S”, and “T”. Note that the answer is not “step”, because the letter “s” must occur in the word twice. Also note that we ignored case for the purposes of comparing whether a letter exists in the word.

Example 2:

Input: licensePlate = “1s3 456”, words = [“looks”, “pest”, “stew”, “show”] Output: “pest” Explanation: There are 3 smallest length words that contains the letters “s”. We return the one that occurred first.

Note:

licensePlate will be a string with length in range [1, 7].

licensePlate will contain digits, spaces, or letters (uppercase or lowercase).

words will have a length in the range [10, 1000].

Every words[i] will consist of lowercase letters, and have length in range [1, 15].

思路: 很暴力,找到一个word符合licensePlate的定义,并不断更新即可。只要licensePlate中出现的字母,word中必须出现,且出现的次数至少相等。采用map计数。

Java版本:

    public String shortestCompletingWord(String licensePlate, String[] words) {
        String ans = "";
        int minLen = 1111;

        int[] map = new int[32];
        for (char c : licensePlate.toLowerCase().toCharArray())
            if (c >= 'a' && c <= 'z') map[c - 'a'] ++;


        for (String word : words) {
            int len = word.length();
            if (contains(map, word)) {
                if (len < minLen) {
                    ans = word;
                    minLen = len;
                }
            }
        }
        return ans;
    }

    boolean contains(int[] map, String word) {
        int[] map2 = new int[32];
        for (char c : word.toLowerCase().toCharArray()) {
            map2[c - 'a'] ++;
        }

        for (int i = 0; i < 32; ++i) {
            if (map[i] != 0) {
                if (map2[i] < map[i]) return false;
            }
        }
        return true;
    }

Python版本:

    def shortestCompletingWord(self, licensePlate, words):
        """
        :type licensePlate: str
        :type words: List[str]
        :rtype: str
        """
        licence_plate = licensePlate.lower()
        d = dict()
        for c in licence_plate:
            if c.isalpha():
                if c not in d:
                    d[c] = 0
                d[c] += 1

        res = None
        length = 1111
        for word in words:
            freq = d.copy()
            for c in word:
                if c in freq:
                    freq[c] -= 1
                    if freq[c] == 0:
                        freq.pop(c)
                if not freq:
                    if len(word) < length:
                        res = word
                        length = len(word)

        return res 

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏菩提树下的杨过

javascript:双链表-插入排序

数组存储前提下,插入排序算法,在最坏情况下,前面的元素需要不断向后移,以便在插入点留出空位,让目标元素插入。 换成链表时,显然无需做这种大量移动,根据每个节点的...

19710
来自专栏数据魔术师

数据结构-线性表|顺序表|链表(下)

1 1 1 哈喽。各位小伙伴好久不见,热心的小编赶在开学季又来给大家送上满满的干货了。祝大家开心快乐! 继上两次咱们聊了顺序表、单链表、静态链表等知识。那么热爱...

2557
来自专栏King_3的技术专栏

leetcode-13-Roman to Integer

2197
来自专栏项勇

笔记29 | 整理Java的容器类

1794
来自专栏xingoo, 一个梦想做发明家的程序员

共享栈

共享栈,即是两个栈使用同一段存储空间。 第一个栈从数组头开始存储,第二个栈从数组尾开始,两个栈向中间拓展。 当top1+1==top2或者top1==top2-...

2007
来自专栏cs

顺序表

724
来自专栏恰同学骚年

剑指Offer面试题:4.从尾到头打印链表

  到解决这个问题肯定要遍历链表。遍历的顺序是从头到尾的顺序,可输出的顺序却是从尾到头。也就是说第一个遍历到的结点最后一个输出,而最后一个遍历到的结点第一个输出...

744
来自专栏猿人谷

Single Number and Single Number II

[1] Given an array of integers, every element appears twice except for one. Find...

1905
来自专栏desperate633

LeetCode Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example...

391
来自专栏ml

CF---(452)A. Eevee

A. Eevee time limit per test 1 second memory limit per test 256 megabytes in...

41611

扫码关注云+社区