<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 0820. 單詞的壓縮編碼 ## 題目地址(820. 單詞的壓縮編碼) <https://leetcode-cn.com/problems/short-encoding-of-words/> ## 題目描述 ``` <pre class="calibre18">``` 給定一個單詞列表,我們將這個列表編碼成一個索引字符串 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 每個單詞都是小寫字母 。 ``` ``` ## 前置知識 - 前綴樹 ## 公司 - 阿里 - 字節 ## 思路 讀完題目之后就發現如果將列表中每一個單詞分別倒序就是一個后綴樹問題。比如 `["time", "me", "bell"]` 倒序之后就是 \["emit", "em", "lleb"\],我們要求的結果無非就是 "emit" 的長度 + "llem"的長度 + "##"的長度(em 和 emit 有公共前綴,計算一個就好了)。 因此符合直覺的想法是使用前綴樹 + 倒序插入的形式來模擬后綴樹。 下面的代碼看起來復雜,但是很多題目我都是用這個模板,稍微調整下細節就能 AC。我這里總結了一套[前綴樹專題](https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md) ![](https://img.kancloud.cn/45/42/4542ce4997102301000b97cd786562cb_850x252.jpg) 前綴樹的 api 主要有以下幾個: - `insert(word)`: 插入一個單詞 - `search(word)`:查找一個單詞是否存在 - `startWith(word)`: 查找是否存在以 word 為前綴的單詞 其中 startWith 是前綴樹最核心的用法,其名稱前綴樹就從這里而來。大家可以先拿 208 題開始,熟悉一下前綴樹,然后再嘗試別的題目。 一個前綴樹大概是這個樣子: ![](https://img.kancloud.cn/08/e0/08e038e4c5e4db4840ad43b89c7ddde5_827x602.jpg) 如圖每一個節點存儲一個字符,然后外加一個控制信息表示是否是單詞結尾,實際使用過程可能會有細微差別,不過變化不大。 這道題需要考慮 edge case, 比如這個列表是 \["time", "time", "me", "bell"\] 這種包含重復元素的情況,這里我使用 hashset 來去重。 ## 關鍵點 - 前綴樹 - 去重 ## 代碼 ``` <pre class="calibre18">``` <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Trie</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span> <span class="hljs-string">""" Initialize your data structure here. """</span> self.Trie = {} <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert</span><span class="hljs-params">(self, word)</span>:</span> <span class="hljs-string">""" Inserts a word into the trie. :type word: str :rtype: void """</span> curr = self.Trie <span class="hljs-keyword">for</span> w <span class="hljs-keyword">in</span> word: <span class="hljs-keyword">if</span> w <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> curr: curr[w] = {} curr = curr[w] curr[<span class="hljs-string">'#'</span>] = <span class="hljs-params">1</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">search</span><span class="hljs-params">(self, word)</span>:</span> <span class="hljs-string">""" Returns if the word is in the trie. :type word: str :rtype: bool """</span> curr = self.Trie <span class="hljs-keyword">for</span> w <span class="hljs-keyword">in</span> word: curr = curr[w] <span class="hljs-title"># len(curr) == 1 means we meet '#'</span> <span class="hljs-title"># when we search 'em'(which reversed from 'me')</span> <span class="hljs-title"># the result is len(curr) > 1</span> <span class="hljs-title"># cause the curr look like { '#': 1, i: {...}}</span> <span class="hljs-keyword">return</span> len(curr) == <span class="hljs-params">1</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">minimumLengthEncoding</span><span class="hljs-params">(self, words: List[str])</span> -> int:</span> trie = Trie() cnt = <span class="hljs-params">0</span> words = set(words) <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> words: trie.insert(word[::<span class="hljs-params">-1</span>]) <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> words: <span class="hljs-keyword">if</span> trie.search(word[::<span class="hljs-params">-1</span>]): cnt += len(word) + <span class="hljs-params">1</span> <span class="hljs-keyword">return</span> cnt ``` ``` ***復雜度分析*** - 時間復雜度:O(N)O(N)O(N),其中 N 為單詞長度列表中的總字符數,比如\["time", "me"\],就是 4 + 2 = 6。 - 空間復雜度:O(N)O(N)O(N),其中 N 為單詞長度列表中的總字符數,比如\["time", "me"\],就是 4 + 2 = 6。 大家也可以關注我的公眾號《力扣加加》獲取更多更新鮮的 LeetCode 題解 ![](https://img.kancloud.cn/a3/63/a363818092b0356fbd67882f0389528b_900x500.jpg) ## 相關題目 - [0208.implement-trie-prefix-tree](208.implement-trie-prefix-tree.html) - [0211.add-and-search-word-data-structure-design](211.add-and-search-word-data-structure-design.html) - [0212.word-search-ii](212.word-search-ii.html) - [0472.concatenated-words](472.concatenated-words.html) - [0820.short-encoding-of-words](https://github.com/azl397985856/leetcode/blob/master/problems/820.short-encoding-of-words.md) - [1032.stream-of-characters](https://github.com/azl397985856/leetcode/blob/master/problems/1032.stream-of-characters.md)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看