|
| 1 | +/// Source : https://leetcode.com/problems/word-abbreviation/ |
| 2 | +/// Author : liuyubobobo |
| 3 | +/// Time : 2022-06-14 |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <vector> |
| 7 | +#include <algorithm> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | + |
| 12 | +/// Hash Map + Simulation |
| 13 | +/// Time Complexity: O(len * 26^2 + nlogn + n * len^2) |
| 14 | +/// Space Compelxity: O(len * 26^2 + n) |
| 15 | +class Solution { |
| 16 | +public: |
| 17 | + vector<string> wordsAbbreviation(const vector<string>& words) { |
| 18 | + |
| 19 | + int n = words.size(); |
| 20 | + |
| 21 | + vector<string> res(n, ""); |
| 22 | + |
| 23 | + vector<int> table[26][26][401]; |
| 24 | + for(int i = 0; i < n; i ++){ |
| 25 | + const string& word = words[i]; |
| 26 | + table[word[0] - 'a'][word.back() - 'a'][word.size()].push_back(i); |
| 27 | + } |
| 28 | + |
| 29 | + for(int first = 0; first < 26; first ++) |
| 30 | + for(int last = 0; last < 26; last ++) |
| 31 | + for(int len = 2; len <= 400; len ++){ |
| 32 | + const vector<int>& v = table[first][last][len]; |
| 33 | + if(v.empty()) continue; |
| 34 | + if(v.size() == 1){ |
| 35 | + res[v[0]] = get_abbr(words[v[0]], 0); |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + vector<pair<string, int>> data(v.size()); |
| 40 | + for(int i = 0; i < v.size(); i ++) data[i] = {words[v[i]], v[i]}; |
| 41 | + sort(data.begin(), data.end()); |
| 42 | + |
| 43 | + for(int i = 0; i < data.size(); i ++){ |
| 44 | + const string& word = data[i].first; |
| 45 | + int index = data[i].second; |
| 46 | + |
| 47 | + for(int prefix_len = 2;; prefix_len ++){ |
| 48 | + bool ok1 = i - 1 < 0 || word.substr(0, prefix_len) != data[i - 1].first.substr(0, prefix_len); |
| 49 | + bool ok2 = i + 1 >= data.size() || word.substr(0, prefix_len) != data[i + 1].first.substr(0, prefix_len); |
| 50 | + if(ok1 && ok2){ |
| 51 | + res[index] = get_abbr(word, prefix_len - 1); |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + assert(res[index] != ""); |
| 56 | + } |
| 57 | + } |
| 58 | + return res; |
| 59 | + } |
| 60 | + |
| 61 | +private: |
| 62 | + // [0...prefix_end_index] + num + last_character |
| 63 | + string get_abbr(const string& s, int prefix_end_index){ |
| 64 | + |
| 65 | + string res = s.substr(0, prefix_end_index + 1); |
| 66 | + int len = s.size() - (prefix_end_index + 2); |
| 67 | + if(len) res += to_string(len); |
| 68 | + res += s.back(); |
| 69 | + |
| 70 | + return res.size() < s.size() ? res : s; |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | + |
| 75 | +void print_vec(const vector<string>& res){ |
| 76 | + for(const string& e: res) cout << e << ' '; cout << '\n'; |
| 77 | +} |
| 78 | + |
| 79 | +int main() { |
| 80 | + |
| 81 | + vector<string> words1 = {"like","god","internal","me","internet","interval","intension","face","intrusion"}; |
| 82 | + print_vec(Solution().wordsAbbreviation(words1)); |
| 83 | + // ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"] |
| 84 | + |
| 85 | + vector<string> words2 = {"abcdefg","abccefg","abcckkg"}; |
| 86 | + print_vec(Solution().wordsAbbreviation(words2)); |
| 87 | + // ["abcd2g","abccefg","abcckkg"] |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
0 commit comments