【Leetcode】 748. 最短补全词

Given a string licensePlate and an array of strings words, find the shortest completing word in words.

A completing word is a word that contains all the letters in licensePlate. Ignore numbers and spaces in licensePlate, and treat letters as case insensitive. If a letter appears more than once in licensePlate, then it must appear in the word the same number of times or more.

For example, if licensePlate = "aBc 12c", then it contains letters 'a', 'b' (ignoring case), and 'c' twice. Possible completing words are "abccdef", "caaacab", and "cbca".

Return the shortest completing word in words. It is guaranteed an answer exists. If there are multiple shortest completing words, return the first one that occurs in words.

Example 1:

Input: licensePlate = "1s3 PSt", words = ["step","steps","stripe","stepple"]
Output: "steps"
Explanation: 
licensePlate contains letters 's', 'p', 's' (ignoring case), and 't'.
"step" contains 't' and 'p', but only contains 1 's'.
"steps" contains 't', 'p', and both 's' characters.
"stripe" is missing an 's'.
"stepple" is missing an 's'.
Since "steps" is the only word containing all the letters, that is the answer.

Example 2:

Input: licensePlate = "1s3 456", words = ["looks","pest","stew","show"]
Output: "pest"
Explanation: 
licensePlate only contains the letter 's'. 
All the words contain 's', but among these "pest", "stew", and "show" are shortest. The answer is "pest" because it is the word that appears earliest of the 3.

Constraints:

1 <= licensePlate.length <= 7
licensePlate contains digits, letters (uppercase or lowercase), or space ' '.
1 <= words.length <= 1000
1 <= words[i].length <= 15
words[i] consists of lower case English letters.

AC:

/*
 * @lc app=leetcode.cn id=748 lang=cpp
 *
 * [748] 最短补全词
 */

// @lc code=start
class Solution {
    
    
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
    
    
        array<int, 26> cnt{
    
    };
        for(char ch : licensePlate)
        {
    
    
            if(isalpha(ch))
            {
    
    
                cnt[tolower(ch) - 'a']++;
            }
        }

        int idx = -1;
        for(int i = 0; i < words.size(); i++)
        {
    
    
            array<int, 26> c{
    
    };
            for(char ch : 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];
    }
};
// @lc code=end

看了半天,超出了自我了预计时间,都还没理解题目意思,直接看官方解析了。
恍然大悟!
原来,步骤这么少:

  • 计算出licensePlate出现的字母次数(同时,使用toslow()函数将大写字母转小写)
  • 遍历word,记录下标,并对其中每个单词出现的次数计数
  • 返回次数不少于licensePlate次数且在有相等的情况下,优先取前面的。

猜你喜欢

转载自blog.csdn.net/qq_54053990/article/details/131032836