Make palindromic string non | CPP Program to rearrange letters of string to find a non - palindromic string if it exists ; Function to print the non - palindromic string if it exists , otherwise prints - 1 ; If all characters are not same , set flag to 1 ; Update frequency of the current character ; If all characters are same ; Print characters in sorted manner ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNonPalinString ( string s ) { int freq [ 26 ] = { 0 } , flag = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] - ' a ' ] ++ ; } if ( ! flag ) cout << " - 1" ; else { for ( int i = 0 ; i < 26 ; i ++ ) for ( int j = 0 ; j < freq [ i ] ; j ++ ) cout << char ( ' a ' + i ) ; } } int main ( ) { string s = " abba " ; findNonPalinString ( s ) ; return 0 ; }