Skip to content

Commit 78eed94

Browse files
committed
0527 solved.
1 parent cd6632d commit 78eed94

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0527)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0527 main.cpp)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
553553
| 524 | [Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/) | [solution](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/solution/) | [C++](0501-1000/0524-Longest-Word-in-Dictionary-through-Deleting/cpp-0524/) | | |
554554
| 525 | [Contiguous Array](https://leetcode.com/problems/contiguous-array/) | [solution](https://leetcode.com/problems/contiguous-array/solution/) [题解](https://leetcode-cn.com/problems/contiguous-array/solution/lian-xu-shu-zu-by-leetcode/) | [C++](0501-1000/0525-Contiguous-Array/cpp-0525/) | | |
555555
| 526 | [Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/) | [solution](https://leetcode.com/problems/beautiful-arrangement/solution/) | [C++](0501-1000/0526-Beautiful-Arrangement/cpp-0526/) | | |
556-
| | | | | | |
556+
| 527 | [Word Abbreviation](https://leetcode.com/problems/word-abbreviation/) | [solution](https://leetcode.com/problems/word-abbreviation/solution/) | [C++](0501-1000/0527-Word-Abbreviation/cpp-0527/) | | |
557557
| 528 | [Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/description/) | [solution](https://leetcode.com/problems/random-pick-with-weight/solution/) | [C++](0501-1000/0528-Random-Pick-with-Weight/cpp-0528/) | | |
558558
| 529 | [Minesweeper](https://leetcode.com/problems/minesweeper/) | [] | [C++](0501-1000/0529-Minesweeper/cpp-0529/) | | |
559559
| 530 | [Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) | [] | | [Java](0501-1000/0530-Minimum-Absolute-Difference-in-BST/java-0530/src/) | |

0 commit comments

Comments
 (0)