Check if string can be rearranged so that every Odd length Substring is Palindrome | C # implementation of the above approach ; Function to check is it possible to rearrange the string such that every odd length substring is palindrome ; Length of the string ; To count number of distinct character in string ; To count frequency of each character ; Inserting into set ; Incrementing the frequency ; All characters in the string are same ; There are more than 2 different character in string ; Currently there is 2 different character in string ; Get the frequencies of the characters that present in string ; Difference between their count is less than or equal to 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool IsPossible ( String s ) { int n = s . Length ; HashSet < char > count = new HashSet < char > ( ) ; Dictionary < char , int > map = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { count . Add ( s [ i ] ) ; if ( map . ContainsKey ( s [ i ] ) ) map [ s [ i ] ] = map [ s [ i ] ] + 1 ; else map . Add ( s [ i ] , 1 ) ; } if ( count . Count == 1 ) return true ; if ( count . Count > 2 ) return false ; String newString = count . ToString ( ) ; int j = 0 ; char it = newString [ j ] ; int x = 0 , y = 0 ; x = ! map . ContainsKey ( it ) ? 0 : map [ it ] ; j ++ ; it = newString [ j ] ; y = ! map . ContainsKey ( it ) ? 0 : map [ it ] ; if ( Math . Abs ( x - y ) <= 1 ) return true ; return false ; } public static void Main ( String [ ] args ) { String s = " aaaddad " ; if ( IsPossible ( s ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }