You are given a string num consisting of digits only.
Return _thelargest 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.
Input: num ="444947137"Output: "7449447"Explanation:
Use the digits "4449477" from "_**44494**_ _**7**_ 13 _**7**_ " to form the palindromic integer "7449447".It can be shown that "7449447"is the largest palindromic integer that can be formed.
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.
To form the largest palindromic number, use the largest available digits for the outer pairs and, if possible, the largest remaining digit for the center. Avoid leading zeros unless the answer is just “0”.
classSolution {
public: string largestPalindromic(string num) {
vector<int> cnt(10);
for (char c : num) cnt[c-'0']++;
string left ="";
for (int d =9; d >=0; --d) {
int pairs = cnt[d] /2;
left += string(pairs, '0'+d);
cnt[d] -=2*pairs;
}
string center ="";
for (int d =9; d >=0; --d) if (cnt[d]) { center = string(1, '0'+d); break; }
string res = left;
reverse(left.begin(), left.end());
res += center + left;
int i =0;
while (i < res.size()-1&& res[i] =='0') ++i;
return res.substr(i);
}
};
classSolution {
public String largestPalindromic(String num) {
int[] cnt =newint[10];
for (char c : num.toCharArray()) cnt[c-'0']++;
StringBuilder left =new StringBuilder();
for (int d = 9; d >= 0; --d) {
int pairs = cnt[d]/ 2;
for (int i = 0; i < pairs; ++i) left.append((char)('0'+d));
cnt[d]-= 2*pairs;
}
String center ="";
for (int d = 9; d >= 0; --d) if (cnt[d]> 0) { center =""+ (char)('0'+d); break; }
String res = left.toString() + center + left.reverse().toString();
int i = 0;
while (i < res.length()-1 && res.charAt(i) =='0') i++;
return res.substring(i);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funlargestPalindromic(num: String): String {
val cnt = IntArray(10)
for (c in num) cnt[c-'0']++val left = StringBuilder()
for (d in9 downTo 0) {
val pairs = cnt[d] / 2 repeat(pairs) { left.append('0'+d) }
cnt[d] -=2*pairs
}
var center = ""for (d in9 downTo 0) if (cnt[d] > 0) { center = ("" + ('0'+d).toChar()); break }
val res = left.toString() + center + left.reverse().toString()
var i = 0while (i < res.length-1&& res[i] =='0') i++return res.substring(i)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
deflargestPalindromic(self, num: str) -> str:
from collections import Counter
cnt = Counter(map(int, num))
left = []
for d in range(9, -1, -1):
pairs = cnt[d] //2 left.extend([str(d)] * pairs)
cnt[d] -=2* pairs
center =''for d in range(9, -1, -1):
if cnt[d]:
center = str(d)
break res =''.join(left) + center +''.join(reversed(left))
i =0while i < len(res)-1and res[i] =='0':
i +=1return res[i:]
impl Solution {
pubfnlargest_palindromic(num: String) -> String {
letmut cnt = [0; 10];
for b in num.bytes() { cnt[(b-b'0') asusize] +=1; }
letmut left = Vec::new();
for d in (0..=9).rev() {
let pairs = cnt[d] /2;
for _ in0..pairs { left.push((b'0'+d asu8) aschar); }
cnt[d] -=2*pairs;
}
letmut center = None;
for d in (0..=9).rev() { if cnt[d] >0 { center = Some((b'0'+d asu8) aschar); break; } }
letmut res = left.clone();
iflet Some(c) = center { res.push(c); }
res.extend(left.iter().rev());
let s: String = res.into_iter().collect();
let i = s.chars().take_while(|&c| c =='0').count();
s[i..].to_string()
}
}