-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path26_Question.cpp
More file actions
62 lines (49 loc) · 1.65 KB
/
26_Question.cpp
File metadata and controls
62 lines (49 loc) · 1.65 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
// Largest Palindromic Number
// You are given a string num consisting of digits only.
// Return the largest palindromic integer (in the form of a string) that can be formed using digits taken from num.
// It should not contain leading zeroes.
// Notes:
// You do not need to use all the digits of num, but you must use at least one digit.
// The digits can be reordered.
// Example 1:
// Input: num = "444947137"
// Output: "7449447"
// Explanation:
// Use the digits "4449477" from "444947137" to form the palindromic integer "7449447".
// It can be shown that "7449447" is the largest palindromic integer that can be formed.
// Example 2:
// Input: num = "00009"
// Output: "9"
// Explanation:
// It can be shown that "9" is the largest palindromic integer that can be formed.
// Note that the integer returned should not contain leading zeroes.
#include<bits/stdc++.h>
using namespace std;
string largestPalindromic(string& num){
if(num.empty()) return "";
vector<int> freq(10,0);
for(char &ch : num){
freq[ch - '0']++;
}
string firstHalf = "", middle = "";
for(char c = '9'; c >= '0'; c--){
if(freq[c - '0'] > 0){
int f = freq[c - '0'];
if(f % 2 == 1 && middle.empty()){
middle +=c;
}
if(c == '0' && firstHalf.empty()) continue;
firstHalf += string(f/2,c);
}
}
if(firstHalf.empty() && middle.empty() && freq['0' - '0'] > 0) return "0";
string last = firstHalf;
reverse(last.begin(), last.end());
return firstHalf+middle+last;
}
int main(){
string num;
cin>>num;
cout<<largestPalindromic(num);
return 0;
}