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 94f9a00

Browse files
Update 527.Word-Abbreviation.cpp
1 parent ab3d422 commit 94f9a00

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed
Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,56 @@
11
class Solution {
22
public:
3-
vector<string> wordsAbbreviation(vector<string>& dict)
4-
{
5-
int n = dict.size();
3+
vector<string> wordsAbbreviation(vector<string>& words)
4+
{
5+
int n = words.size();
66
vector<string>rets(n);
7-
unordered_set<int>Set;
7+
8+
vector<int>Set;
89
for (int i=0; i<n; i++)
9-
Set.insert(i);
10+
Set.push_back(i);
1011

1112
int abbrNum = 1;
1213
while (1)
1314
{
14-
unordered_map<string,vector<int>>Map;
15-
for (auto idx : Set)
15+
unordered_map<string, vector<int>> Map;
16+
17+
for (int idx: Set)
1618
{
17-
string abbr=getAbbr(dict[idx], abbrNum);
19+
string abbr = getAbbr(words[idx], abbrNum);
1820
Map[abbr].push_back(idx);
1921
}
2022
Set.clear();
2123

22-
for (auto& [str, indices]:Map)
24+
for (auto& [abbr, indices]:Map)
2325
{
24-
if (indices.size()>1)
26+
if (indices.size() > 1)
2527
{
2628
for (int idx: indices)
27-
Set.insert(idx);
28-
}
29+
Set.push_back(idx);
30+
}
2931
else
30-
{
31-
rets[indices[0]] = str;
32-
}
32+
rets[indices[0]] = abbr;
3333
}
3434

35-
if (Set.size()==0) break;
36-
abbrNum++;
35+
abbrNum += 1;
36+
if (Set.size() == 0)
37+
break;
3738
}
3839

3940
return rets;
40-
4141
}
4242

4343
string getAbbr(string s, int abbrNum)
44-
{
44+
{
45+
if (s.size() < 3) return s;
46+
4547
string t;
46-
if (s.size()<=2)
47-
{
48-
t=s;
49-
return t;
50-
}
48+
t = s.substr(0, abbrNum);
49+
t += to_string(s.size() - abbrNum - 1);
50+
t += s.back();
51+
52+
if (t.size() == s.size()) return s;
5153

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;
54+
return t;
5755
}
5856
};

0 commit comments

Comments
(0)

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