LeetCode 820. 単語の圧縮符号化Short Encoding of Words


Table of Contents
一、中国語版
二、英語版
三、My answer
四、問題解決報告
 

一、中国語版


単語リストを与え、このリストをインデックス文字列SとインデックスリストAとに符号化する.
たとえば、このリストが["time","me","bell"]の場合、S="time#bell#",indexes=[0,2,5]と表すことができます.
各インデックスについては、文字列S内のインデックスの位置から文字列の読み取りを開始し、「#」が終了するまで、前の単語リストを復元できます.
では、与えられた単語リストを符号化することに成功した最小文字列の長さはどのくらいですか.
 
例:
入力:words=["time","me","bell"]出力:10説明:S="time#bell#",indexes=[0,2,5]. 
ヒント:
1 <= words.length <= 2000 1 <= words[i].length<=7各単語は小文字です.
 

二、英語版


Given a list of words, we may encode it by writing a reference string S and a list of indexes A.
For example, if the list of words is ["time", "me", "bell"], we can write it as S = "time#bell#" and indexes = [0, 2, 5].
Then for each index, we will recover the word by reading from the reference string from that index until we reach a "#"character.
What is the length of the shortest reference string S possible that encodes the given words?
Example:
Input: words = ["time", "me", "bell"] Output: 10 Explanation: S = "time#bell#"and indexes = [0, 2, 5].  
Note:
1 <= words.length <= 2000. 1 <= words[i].length <= 7. Each word has only lowercase letters.
ソース:力ボタン(LeetCode)リンク:https://leetcode-cn.com/problems/short-encoding-of-words著作権はインターネットの所有に帰属する.商業転載は公式の授権に連絡してください.非商業転載は出典を明記してください.

三、My answer


本題はLeetCodeの公式問題解を採用し、以下のように記録する.
class Solution:
    def minimumLengthEncoding(self, words: List[str]) -> int:
        good = set(words)
        for word in words:
            for k in range(1, len(word)):
                good.discard(word[k:])

        return sum(len(word) + 1 for word in good)

 :LeetCode-Solution
 :https://leetcode-cn.com/problems/short-encoding-of-words/solution/dan-ci-de-ya-suo-bian-ma-by-leetcode-solution/
 : (LeetCode)
 。 , 。

四、問題解決報告


公式の問題解を記録するのは、コードが特にpythonicなので、学ぶ価値があります.
例えばsetのdiscard()関数:https://www.runoob.com/python3/ref-set-discard.html
問題を解く構想の動図は以下の通りである.