#K46947. Rearrange to Palindrome via Substring Reversal
Rearrange to Palindrome via Substring Reversal
Rearrange to Palindrome via Substring Reversal
You are given a string s
of length n consisting only of lowercase English letters. Your task is to determine whether it is possible to rearrange the string into a palindrome by reversing any number of non-empty contiguous substrings.
A string is a palindrome if it reads the same backward as forward. With the allowed operation, you can reverse any contiguous segment of the string as many times as needed. Effectively, this operation allows you to reorder the string arbitrarily. Therefore, the key observation is that a string can be rearranged into a palindrome if and only if the frequency of its characters meets the palindrome condition: at most one character may occur an odd number of times.
Input Constraint: 1 ≤ |s| ≤ 105.
inputFormat
The input consists of a single line containing the string s
.
outputFormat
Output YES
if it is possible to rearrange s
into a palindrome; otherwise, output NO
.
racecar
YES
</p>