Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab3d422

Browse files
Update and rename 527.Word Abbreviation.cpp to 527.Word-Abbreviation.cpp
1 parent 73253bc commit ab3d422

File tree

2 files changed

+58
-63
lines changed

2 files changed

+58
-63
lines changed

‎String/527.Word-Abbreviation/527.Word Abbreviation.cpp‎

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution {
2+
public:
3+
vector<string> wordsAbbreviation(vector<string>& dict)
4+
{
5+
int n = dict.size();
6+
vector<string>rets(n);
7+
unordered_set<int>Set;
8+
for (int i=0; i<n; i++)
9+
Set.insert(i);
10+
11+
int abbrNum = 1;
12+
while (1)
13+
{
14+
unordered_map<string,vector<int>>Map;
15+
for (auto idx : Set)
16+
{
17+
string abbr=getAbbr(dict[idx], abbrNum);
18+
Map[abbr].push_back(idx);
19+
}
20+
Set.clear();
21+
22+
for (auto& [str, indices]:Map)
23+
{
24+
if (indices.size()>1)
25+
{
26+
for (int idx: indices)
27+
Set.insert(idx);
28+
}
29+
else
30+
{
31+
rets[indices[0]] = str;
32+
}
33+
}
34+
35+
if (Set.size()==0) break;
36+
abbrNum++;
37+
}
38+
39+
return rets;
40+
41+
}
42+
43+
string getAbbr(string s, int abbrNum)
44+
{
45+
string t;
46+
if (s.size()<=2)
47+
{
48+
t=s;
49+
return t;
50+
}
51+
52+
t=s.substr(0, abbrNum);
53+
t+=to_string(s.size()-abbrNum-1);
54+
t+=s.back();
55+
if (t.size()==s.size()) t=s;
56+
return t;
57+
}
58+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /