About LeetCode enhance problem-solving (b)

Today encounter a problem, I have to say, the idea is everything, to share my analysis and problem-solving mentality and feelings of problem-solving way of big brother.

 

820. The word compression coding

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

 

Given a list of words, this list will be encoded as a string S index and an index list A.

For example, if the list is [ "time", "me", "bell"], we can be expressed as S = "time # bell #" and indexes = [0, 2, 5].

For each index, we can start by reading the string index from the string S in position until the "#" end, to restore our previous list of words.

Then the minimum length of the string to the success of a given word list for encoding is how much?

 

Example:

Input: words = [ "time", "me", "bell"]
Output: 10
Description: S = "time # bell # ", indexes = [0, 2, 5].
 

prompt:

. 1 <= words.length <= 2000
. 1 <= words [I] .length <=. 7
each word lowercase.

 

 

Concise explanation about the subject requirements: For example, time and me, because me it is time to remove the suffix, because you can find me # From the time #, no established time # me # again, thus forming a shortest character string length.

 

My idea: a start building a collection (the purpose of de-emphasis), the string of words into the set, then access to the collection, delete the current string of suffixes. Finally, add the collection (the length of all strings +1), it is the final length of the shortest. After the write operation was past, but more slowly, looked after the official parsing code optimization as follows (substr that actually can be used to delete a suffix, or unskilled ah).

 

Of course, this problem can be solved with a dictionary tree, but the complexity of the space will be relatively large (in fact, like concise code).

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

 

High complexity, is exponential.

Gangster below to share ideas (amazing to).

 

First, no matter what language, the string sorting containers always in accordance with the order to the dictionary, then we can find a rule, all the strings in reverse order, such as: time -> emit me-> em dell-> lled, then we reordering, is not necessarily in front of em emit, we can not find em easier, and delete. Whether any string, you can rule out one by one according to the dictionary order, the last remaining string suffix does not exist.

 

Gangster Code:

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

Code links: https://leetcode-cn.com/problems/short-encoding-of-words/solution/dan-ci-de-ya-suo-bian-ma-by-leetcode-solution/313014

 

 

I can only say that the idea is too strong.

 

Punch: encountered heavy string to go suffix, first set of thinking, the dictionary tree, then reverse lexicographical order.

Guess you like

Origin www.cnblogs.com/xiangqi/p/12588765.html