Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
libterty committed Mar 6, 2021
1 parent 86fb677 commit 805a6cf
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Short-Encoding-Words/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <vector>
#include <sstream>
#include <unordered_set>

using namespace std;

/**
* @see https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3662/
*/
class Solution {
public:
int minimumLengthEncoding(vector<string>& words) {
int ans = 0;
unordered_set<string> us(words.begin(), words.end());
for (string word : us) {
for(int i = 1; i < word.size(); ++i) {
us.erase(word.substr(i));
}
}
for (string word : us) ans += word.size() + 1;
return ans;
}
};

template <class T>
vector<T> write_vector(vector<T> &v, string &temp, istringstream &ss) {
while (ss >> temp)
v.push_back(temp);
return v;
}

int main() {
string line;
vector<string> words;
string temp;
getline(cin, line);
istringstream ss(line);
words = write_vector(words, temp, ss);
Solution sol;
int r = sol.minimumLengthEncoding(words);
cout << r << endl;

return 0;
}

0 comments on commit 805a6cf

Please sign in to comment.