Given a string s, determine whether its characters can be rearranged to form a palindrome. Return True if such a rearrangement exists, otherwise return False.
A palindrome reads the same forward and backward. The rearrangement must use every character of s exactly once.
Function Signature
def can_rearrange_palindrome(s: str) -> bool:
Constraints
-
1 <= len(s) <= 10^5
-
s
consists of lowercase English letters only.
Examples
Input: s = "civic"
Output: True
"civic" is already a palindrome, so a valid arrangement exists. Its letters are c twice, i twice and v once, and the single v sits in the middle.
Input: s = "hello"
Output: False
h, e and o each appear once and l appears twice. Each lone letter would need a matching copy at the mirrored position, and a palindrome has only one unpaired spot (its middle), so no arrangement works.
Input: s = "aabbc"
Output: True
"aabbc" is not a palindrome as written, but it can be rearranged into "abcba".