diff options
Diffstat (limited to 'introductory_problems/palindrome_reorder.cpp')
-rw-r--r-- | introductory_problems/palindrome_reorder.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/introductory_problems/palindrome_reorder.cpp b/introductory_problems/palindrome_reorder.cpp new file mode 100644 index 0000000..8e0a602 --- /dev/null +++ b/introductory_problems/palindrome_reorder.cpp @@ -0,0 +1,28 @@ +// 1755. Palindrome Reorder +#include <bits/stdc++.h> +using namespace std; + +int main() { + static char s[1000001]; + scanf("%s", s); + static int n = strlen(s), odd = -1, freq[26]; + for (int i = 0; i < n; i++) + freq[s[i] - 'A']++; + for (int i = 0; i < 26; i++) { + if (freq[i] & 1) { + if (odd != -1) { + printf("NO SOLUTION\n"); + } else { + odd = i; + } + } + } + for (int i = 0; i < 26; i++) + for (int j = 0; j < freq[i] / 2; j++) + printf("%c", i + 'A'); + if (odd != -1) + printf("%c", odd + 'A'); + for (int i = 25; i >= 0; i--) + for (int j = 0; j < freq[i] / 2; j++) + printf("%c", i + 'A'); +} |