Skip to content

Commit fc60b53

Browse files
authored
Create 2131.Longest-Palindrome-by-Concatenating-Two-Letter-Words.cpp
1 parent b149fef commit fc60b53

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
int longestPalindrome(vector<string>& words)
4+
{
5+
unordered_map<string,int>count1;
6+
unordered_map<string,int>count2;
7+
unordered_map<string,int>count3;
8+
9+
for (auto str: words)
10+
{
11+
string str2 = str;
12+
reverse(str2.begin(), str2.end());
13+
14+
if (str2==str)
15+
count3[str]++;
16+
else
17+
{
18+
string key = min(str2, str);
19+
if (key==str)
20+
count1[key]++;
21+
else
22+
count2[key]++;
23+
}
24+
}
25+
26+
int ret = 0;
27+
for (auto& [key, val]: count1)
28+
{
29+
int a = count1[key];
30+
int b = count2[key];
31+
ret += min(a,b)*2*2;
32+
}
33+
int flag = 0;
34+
for (auto& [key, val]: count3)
35+
{
36+
ret += val/2*2*2;
37+
if (val%2==1)
38+
flag = 1;
39+
}
40+
41+
return ret+flag*2;
42+
}
43+
};

0 commit comments

Comments
 (0)