资讯专栏INFORMATION COLUMN

Leetcode PHP题解--D86 748. Shortest Completing Word

seasonley / 1013人阅读

摘要:题目链接题目分析从给定的一个字符串中提取字符。若出现次数相同,则返回第一个符合条件的单词。假定结果必定存在。思路先提取字符,转换成小写,并计算字符出现的次数。短则覆盖,长则抛弃。最终代码若觉得本文章对你有用,欢迎用爱发电资助。

D86 748. Shortest Completing Word 题目链接

748. Shortest Completing Word

题目分析

从给定的一个字符串中提取字符。从另一个给定的单词数组中,选择出所提取的字符在单词中出现次数相等或大于的单词。若出现次数相同,则返回第一个符合条件的单词。

假定结果必定存在。

思路

先提取字符,转换成小写,并计算字符出现的次数。

遍历数组中的每一个单词,先计算单词中每个字符出现的次数。

同时,遍历前面计算的字符出现次数,若有任何一个字符没有在当前单词中没出现,那么可以抛弃当前单词。
若出现次数小于前面计算的出现次数,也可以排除。

若出现了符合的单词,先判断和原先保存的单词长度是否短。
短则覆盖,长则抛弃。

最终代码
="a" && $val<="z")||($val>="A" && $val<="Z")){
                if(!isset($plateCounts[$val])){
                    $plateCounts[$val] = 0;
                }
                $plateCounts[$val] += 1;
            }
        };
        $match = null;
            foreach($words as $word){
        $wordCounts = array_count_values(str_split($word));
        $failed = false;
        foreach($plateCounts as $char => $amount){
            if(!isset($wordCounts[$char])){
                $failed = true;
                break;
            }
            if($amount > $wordCounts[$char]){
                $failed = true;
                break;
            }
        }
        if(!$failed){
            if(is_null($match)){
                $match = $word;
            }
            else{
                if(strlen($match)>strlen($word)){
                    $match = $word;
                }
            }
        }
    }
        return $match;
    }
}

若觉得本文章对你有用,欢迎用爱发电资助。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/31716.html

相关文章

  • Leetcode PHP题解--D49 821. Shortest Distance to a Ch

    摘要:返回字符串中每一个字符离给定的字符的最短距离。否则,当当前下标大于上一个出现字符的位置,且存在下一个字符时,距离为两者中最小的那个。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D49 821. Shortest Distance to a Character 题目链接 821. Shortest Distance to a Character 题目分析 给定一个字符串s和一个字符...

    Shisui 评论0 收藏0
  • [Leetcode] Shortest Word Distance 最短单词间距

    摘要:代码第一次写入就先不比较第一次写入就先不比较哈希表法复杂度时间空间思路因为会多次调用,我们不能每次调用的时候再把这两个单词的下标找出来。我们可以用一个哈希表,在传入字符串数组时,就把每个单词的下标找出存入表中。 Shortest Word Distance Given a list of words and two words word1 and word2, return the ...

    jsliang 评论0 收藏0
  • [LeetCode] 244. Shortest Word Distance II

    Problem Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the l...

    Nekron 评论0 收藏0
  • [LeetCode] 245. Shortest Word Distance III

    Problem Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. word1 and word2 may be the same and they represent two individual words i...

    csRyan 评论0 收藏0
  • [LeetCode] 243. Shortest Word Distance

    Problem Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. Example:Assume that words = [practice, makes, perfect, coding, makes]. In...

    高胜山 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<