-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path409-longest-palindrome.cpp
More file actions
90 lines (82 loc) · 3.34 KB
/
409-longest-palindrome.cpp
File metadata and controls
90 lines (82 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <algorithm>
// performance measure header
#include <iomanip>
#include <chrono>
using namespace std;
// first solution - wrong approach
/* class Solution {
public:
int longestPalindrome(string s) {
map<char, int> letters;
int count{0}, max_odd{0};
for (const char c : s)
++letters[c];
for (const auto& c : letters)
if (c.second % 2 == 0)
count += c.second;
else
c.second > max_odd ? max_odd = c.second : max_odd = max_odd;
return count + max_odd;
}
}; */
// second solution - after checking the code of the master
/* class Solution {
public:
int longestPalindrome(string s) {
map<char, int> letters;
int count{0};
for (const char c : s)
++letters[c];
for (const auto& c : letters)
if (c.second % 2 == 0)
count += c.second;
return min(int(s.size()), count + 1);
}
}; */
// solution from a master
/* class Solution {
public:
int longestPalindrome(string s) {
int odd{0};
unordered_map<char, int>m;
for(auto c : s)
odd += m[c]++ % 2 ? -1 : 1;
return min(s.size(), s.size() - odd + 1);
}
}; */
// rewriting the master solution in order to understand all the steps
class Solution {
public:
int longestPalindrome(string s) {
int odd{0};
unordered_map<char, int>m;
sort(s.begin(), s.end());
for (auto c : s)
++m[c];
for (auto &val : m) {
if (val.second % 2)
odd += 1;
}
return min(s.size(), s.size() - odd + 1);
}
};
int main() {
Solution solution;
//string s{"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"};
string s;
cout << "Insert a string: ";
cin >> s;
cout << s.size() << '\n';
auto t1{chrono::high_resolution_clock::now()};
cout << solution.longestPalindrome(s) << '\n';
auto t2{chrono::high_resolution_clock::now()};
chrono::duration<double, milli> ms_double{t2-t1};
cout << setprecision(17) << ms_double.count() << " ms\n";
sort(s.begin(), s.end());
cout << s << '\n';
return 0;
}