Generate all possible strings formed by replacing letters with given respective symbols | Java program for the above approach ; Function to generate all possible string by replacing the characters with mapped symbols ; Base Case ; Function call with the P - th character not replaced ; Replace the P - th character ; Function call with the P - th character replaced ; Driver Code ; Function Call
import java . util . HashMap ; class GFG { public static void generateLetters ( String S , int P , HashMap < Character , Character > M ) { if ( P == S . length ( ) ) { System . out . println ( S ) ; return ; } generateLetters ( S , P + 1 , M ) ; S = S . substring ( 0 , P ) + M . get ( S . charAt ( P ) ) + S . substring ( P + 1 ) ; generateLetters ( S , P + 1 , M ) ; return ; } public static void main ( String args [ ] ) { String S = " aBc " ; HashMap < Character , Character > M = new HashMap < Character , Character > ( ) ; M . put ( ' a ' , ' $ ' ) ; M . put ( ' B ' , ' # ' ) ; M . put ( ' c ' , ' ^ ' ) ; M . put ( ' d ' , ' & ' ) ; M . put ( '1' , ' * ' ) ; M . put ( '2' , ' ! ' ) ; M . put ( ' E ' , ' @ ' ) ; generateLetters ( S , 0 , M ) ; } }