text: Palindromic strings of length 3 possible by using characters of a given string | C # program for the above approach ; Function to print all palindromic strings of length 3 that can be formed using characters of string S ; Stores the count of character ; Traverse the string S ; Stores all palindromic strings ; Iterate over the charchaters over the range [ ' a ' , ' z ' ] ; If Hash [ ch ] is equal to 2 ; Iterate over the characters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; Push the s into the set st ; If Hash [ i ] is greater than or equal to 3 ; Iterate over charchaters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; If Hash [ j ] is positive ; Push s into the set st ; Iterate over the set ; Driver Code
code: using System ; using System . Collections . Generic ; class GFG { static void generatePalindrome ( string S ) { Dictionary < char , int > Hash = new Dictionary < char , int > ( ) ; foreach ( char ch in S ) { if ( Hash . ContainsKey ( ch ) ) Hash [ ch ] ++ ; else Hash . Add ( ch , 1 ) ; } HashSet < string > st = new HashSet < string > ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( Hash . ContainsKey ( i ) && Hash [ i ] == 2 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { string s = " " ; if ( Hash . ContainsKey ( j ) && i != j ) { s += i ; s += j ; s += i ; st . Add ( s ) ; } } } if ( Hash . ContainsKey ( i ) && Hash [ i ] >= 3 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { string s = " " ; if ( Hash . ContainsKey ( j ) ) { s += i ; s += j ; s += i ; st . Add ( s ) ; } } } } foreach ( string ans in st ) { Console . WriteLine ( ans ) ; } } public static void Main ( ) { string S = " ddabdac " ; generatePalindrome ( S ) ; } }