Medium
You are given a palindromic string s
.
Return the lexicographically smallest palindromic permutation of s
.
Example 1:
Input: s = “z”
Output: “z”
Explanation:
A string of only one character is already the lexicographically smallest palindrome.
Example 2:
Input: s = “babab”
Output: “abbba”
Explanation:
Rearranging "babab"
→ "abbba"
gives the smallest lexicographic palindrome.
Example 3:
Input: s = “daccad”
Output: “acddca”
Explanation:
Rearranging "daccad"
→ "acddca"
gives the smallest lexicographic palindrome.
Constraints:
1 <= s.length <= 105
s
consists of lowercase English letters.s
is guaranteed to be palindromic.class Solution {
fun smallestPalindrome(s: String): String {
val n = s.length
val m = n / 2
if (n == 1 || n == 2) {
return s
}
val fArr = s.substring(0, m).toCharArray()
fArr.sort()
var f = String(fArr)
val rev = StringBuilder(f).reverse()
if (n % 2 == 1) {
f += s[m]
}
return f + rev
}
}