[LeetCode] Short Encoding of Words 單詞集的短編碼


 

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. 1 <= words.length <= 2000.
  2. 1 <= words[i].length <= 7.
  3. Each word has only lowercase letters.

 

這道題給了我們一個單詞數組,讓我們對其編碼,不同的單詞之間加入#號,每個單詞的起點放在一個坐標數組內,終點就是#號,能合並的單詞要進行合並,問輸入字符串的最短長度。題意不難理解,難點在於如何合並單詞,我們觀察題目的那個例子,me和time是能夠合並的,只要標清楚其實位置,time的起始位置是0,me的起始位置是2,那么根據#號位置的不同就可以順利的取出me和time。需要注意的是,如果me換成im,或者tim的話,就不能合並了,因為我們是要從起始位置到#號之前所有的字符都要取出來。搞清楚了這一點之后,我們在接着觀察,由於me是包含在time中的,所以我們處理的順序應該是先有time#,然后再看能否包含me,而不是先生成了me#之后再處理time,所以我們可以得出結論,應該先處理長單詞,那么就給單詞數組按長度排序一下就行,自己重寫一個comparator就行。然后我們遍歷數組,對於每個單詞,我們都在編碼字符串查找一下,如果沒有的話,直接加上這個單詞,再加一個#號,如果有的話,就可以得到出現的位置。比如在time#中查找me,得到found=2,然后我們要驗證該單詞后面是否緊跟着一個#號,所以我們直接訪問found+word.size()這個位置,如果不是#號,說明不能合並,我們還是要加上這個單詞和#號。最后返回編碼字符串的長度即可,參見代碼如下:

 

解法一:

class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        string str = "";
        sort(words.begin(), words.end(), [](string& a, string& b){return a.size() > b.size();});
        for (string word : words) {
            int found = str.find(word);
            if (found == string::npos || str[found + word.size()] != '#') {
                str += word + "#";
            }
        }
        return str.size();
    }
};

 

我們再來看一種不用自定義comparator的方法,根據之前的分析,我們知道其實是在找單詞的后綴,比如me就是time的后綴。我們希望將能合並的單詞排在一起,比較好處理,而后綴又不好排序。那么我們就將其轉為前綴,做法就是給每個單詞翻轉一下,time變成emit,me變成em,這樣我們只要用默認的字母順序排,就可以得到em,emit的順序,那么能合並的單詞就放到一起了,而且一定是當前的合並到后面一個,那么就好做很多了。我們只要判讀當前單詞是否是緊跟着的單詞的前綴,是的話就加0,不是的話就要加上當前單詞的長度並再加1,多加的1是#號。判斷前綴的方法很簡單,直接在后面的單詞中取相同長度的前綴比較就行了。由於我們每次都要取下一個單詞,為了防止越界,只處理到倒數第二個單詞,那么就要把最后一個單詞的長度加入結果res,並再加1即可,參見代碼如下:

 

解法二:

class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        int res = 0, n = words.size();
        for (int i = 0; i < n; ++i) reverse(words[i].begin(), words[i].end());
        sort(words.begin(), words.end());
        for (int i = 0; i < n - 1; ++i) {
            res += (words[i] == words[i + 1].substr(0, words[i].size())) ? 0  : words[i].size() + 1;
        }
        return res + words.back().size() + 1;
    }
};

 

接下來的這種方法也很巧妙,用了一個HashSet,將所有的單詞先放到這個HashSet中。原理是對於每個單詞,我們遍歷其所有的后綴,比如time,那么就遍歷ime,me,e,然后看HashSet中是否存在這些后綴,有的話就刪掉,那么HashSet中的me就會被刪掉,這樣保證了留下來的單詞不可能再合並了,最后再加上每個單詞的長度到結果res,並且同時要加上#號的長度,參見代碼如下:

 

解法三:

class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        int res = 0;
        unordered_set<string> st(words.begin(), words.end());
        for (string word : st) {
            for (int i = 1; i < word.size(); ++i) {
                st.erase(word.substr(i));
            }
        }
        for (string word : st) res += word.size() + 1;
        return res;
    }
};

 

參考資料:

https://leetcode.com/problems/short-encoding-of-words/

https://leetcode.com/problems/short-encoding-of-words/discuss/125825/Easy-to-understand-Java-solution

https://leetcode.com/problems/short-encoding-of-words/discuss/125822/C%2B%2B-4-lines-reverse-and-sort

https://leetcode.com/problems/short-encoding-of-words/discuss/125811/C%2B%2BJavaPython-Easy-Understood-Solution-with-Explanation

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM