成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

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

seasonley / 1193人閱讀

摘要:題目鏈接題目分析從給定的一個(gè)字符串中提取字符。若出現(xiàn)次數(shù)相同,則返回第一個(gè)符合條件的單詞。假定結(jié)果必定存在。思路先提取字符,轉(zhuǎn)換成小寫,并計(jì)算字符出現(xiàn)的次數(shù)。短則覆蓋,長則拋棄。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。

D86 748. Shortest Completing Word 題目鏈接

748. Shortest Completing Word

題目分析

從給定的一個(gè)字符串中提取字符。從另一個(gè)給定的單詞數(shù)組中,選擇出所提取的字符在單詞中出現(xiàn)次數(shù)相等或大于的單詞。若出現(xiàn)次數(shù)相同,則返回第一個(gè)符合條件的單詞。

假定結(jié)果必定存在。

思路

先提取字符,轉(zhuǎn)換成小寫,并計(jì)算字符出現(xiàn)的次數(shù)。

遍歷數(shù)組中的每一個(gè)單詞,先計(jì)算單詞中每個(gè)字符出現(xiàn)的次數(shù)。

同時(shí),遍歷前面計(jì)算的字符出現(xiàn)次數(shù),若有任何一個(gè)字符沒有在當(dāng)前單詞中沒出現(xiàn),那么可以拋棄當(dāng)前單詞。
若出現(xiàn)次數(shù)小于前面計(jì)算的出現(xiàn)次數(shù),也可以排除。

若出現(xiàn)了符合的單詞,先判斷和原先保存的單詞長度是否短。
短則覆蓋,長則拋棄。

最終代碼
="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;
    }
}

若覺得本文章對你有用,歡迎用愛發(fā)電資助。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/31716.html

相關(guān)文章

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

    摘要:返回字符串中每一個(gè)字符離給定的字符的最短距離。否則,當(dāng)當(dāng)前下標(biāo)大于上一個(gè)出現(xiàn)字符的位置,且存在下一個(gè)字符時(shí),距離為兩者中最小的那個(gè)。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 D49 821. Shortest Distance to a Character 題目鏈接 821. Shortest Distance to a Character 題目分析 給定一個(gè)字符串s和一個(gè)字符...

    Shisui 評論0 收藏0
  • [Leetcode] Shortest Word Distance 最短單詞間距

    摘要:代碼第一次寫入就先不比較第一次寫入就先不比較哈希表法復(fù)雜度時(shí)間空間思路因?yàn)闀啻握{(diào)用,我們不能每次調(diào)用的時(shí)候再把這兩個(gè)單詞的下標(biāo)找出來。我們可以用一個(gè)哈希表,在傳入字符串?dāng)?shù)組時(shí),就把每個(gè)單詞的下標(biāo)找出存入表中。 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

發(fā)表評論

0條評論

閱讀需要支付1元查看
<