text: Palindromic strings of length 3 possible by using characters of a given string | Java 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: import java . util . * ; public class Main { static void generatePalindrome ( String S ) { HashMap < Character , Integer > Hash = new HashMap < > ( ) ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( Hash . containsKey ( S . charAt ( i ) ) ) Hash . put ( S . charAt ( i ) , Hash . get ( S . charAt ( i ) ) + 1 ) ; else Hash . put ( S . charAt ( i ) , 1 ) ; } TreeSet < String > st = new TreeSet < String > ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( Hash . containsKey ( i ) && Hash . get ( 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 . get ( i ) >= 3 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { String s = " " ; if ( Hash . containsKey ( j ) ) { s += i ; s += j ; s += i ; st . add ( s ) ; } } } } for ( String ans : st ) { System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { String S = " ddabdac " ; generatePalindrome ( S ) ; } }