codeparrot/xlcost-text-to-code huggingface dataset:
Find all distinct palindromic sub | Java program to find all distinct palindrome sub - strings of a given string ; Function to print all distinct palindrome sub - strings of s ; table for storing results ( 2 rows for odd - and even - length palindromes ; Find all sub - string palindromes from the given input string insert ' guards ' to iterate easily over s ; int rp = 0 ; length of ' palindrome ▁ radius ' ; Attempt to expand palindrome centered at i ; Assigning the found palindromic length to odd / even length array ; remove ' guards ' ; Put all obtained palindromes in a hash map to find only distinct palindromess ; printing all distinct palindromes from hash map ; Driver program
import java . util . Map ; import java . util . TreeMap ; public class GFG { static void palindromeSubStrs ( String s ) { TreeMap < String , Integer > m = new TreeMap < > ( ) ; int n = s . length ( ) ; int [ ] [ ] R = new int [ 2 ] [ n + 1 ] ; s = " @ " + s + " # " ; for ( int j = 0 ; j <= 1 ; j ++ ) { R [ j ] [ 0 ] = 0 ; int i = 1 ; while ( i <= n ) { while ( s . charAt ( i - rp - 1 ) == s . charAt ( i + j + rp ) ) R [ j ] [ i ] = rp ; int k = 1 ; while ( ( R [ j ] [ i - k ] != rp - k ) && ( k < rp ) ) { R [ j ] [ i + k ] = Math . min ( R [ j ] [ i - k ] , rp - k ) ; k ++ ; } rp = Math . max ( rp - k , 0 ) ; i += k ; } } s = s . substring ( 1 , s . length ( ) - 1 ) ; m . put ( s . substring ( 0 , 1 ) , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= 1 ; j ++ ) for ( int rp = R [ j ] [ i ] ; rp > 0 ; rp -- ) m . put ( s . substring ( i - rp - 1 , i - rp - 1 + 2 * rp + j ) , 1 ) ; m . put ( s . substring ( i , i + 1 ) , 1 ) ; } System . out . println ( " Below ▁ are ▁ " + ( m . size ( ) ) + " ▁ palindrome ▁ sub - strings " ) ; for ( Map . Entry < String , Integer > ii : m . entrySet ( ) ) System . out . println ( ii . getKey ( ) ) ; } public static void main ( String args [ ] ) { palindromeSubStrs ( " abaaa " ) ; } }
Largest value in each level of Binary Tree | Set | Java implementation to print largest value in each level of Binary Tree ; structure of a node of binary tree ; function to get a new node ; allocate space ; put in the data ; function to print largest value in each level of Binary Tree ; if tree is empty ; push root to the queue ' q ' ; node count for the current level ; if true then all the nodes of the tree have been traversed ; maximum element for the current level ; get the front element from ' q ' ; remove front element from ' q ' ; if true , then update ' max ' ; if left child exists ; if right child exists ; print maximum element of current level ; Driver code ; Construct a Binary Tree 4 / \ 9 2 / \ \ 3 5 7 ; Function call
import java . util . * ; class GfG { static class Node { int data ; Node left = null ; Node right = null ; } static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = null ; temp . right = null ; return temp ; } static void largestValueInEachLevel ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; int nc , max ; q . add ( root ) ; while ( true ) { nc = q . size ( ) ; if ( nc == 0 ) break ; max = Integer . MIN_VALUE ; while ( nc != 0 ) { Node front = q . peek ( ) ; q . remove ( ) ; if ( max < front . data ) max = front . data ; if ( front . left != null ) q . add ( front . left ) ; if ( front . right != null ) q . add ( front . right ) ; nc -- ; } System . out . println ( max + " ▁ " ) ; } } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 4 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 7 ) ; largestValueInEachLevel ( root ) ; } }
Populate Inorder Successor for all nodes | A binary tree node ; Set next of p and all descendants of p by traversing them in reverse Inorder ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in left subtree ; Constructed binary tree is 10 / \ 8 12 / 3 ; Populates nextRight pointer in all nodes ; Let us see the populated values ; - 1 is printed if there is no successor
class Node { int data ; Node left , right , next ; Node ( int item ) { data = item ; left = right = next = null ; } } class BinaryTree { Node root ; static Node next = null ; void populateNext ( Node node ) { if ( node != null ) { populateNext ( node . right ) ; node . next = next ; next = node ; populateNext ( node . left ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 3 ) ; tree . populateNext ( tree . root ) ; Node ptr = tree . root . left . left ; while ( ptr != null ) { int print = ptr . next != null ? ptr . next . data : - 1 ; System . out . println ( " Next ▁ of ▁ " + ptr . data + " ▁ is : ▁ " + print ) ; ptr = ptr . next ; } } }
Maximum Product Cutting | DP | Java program to find maximum product ; The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver program to test above functions
import java . io . * ; class GFG { static int maxProd ( int n ) { if ( n == 2 n == 3 ) return ( n - 1 ) ; int res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } public static void main ( String [ ] args ) { System . out . println ( " Maximum ▁ Product ▁ is ▁ " + maxProd ( 10 ) ) ; } }
Dice Throw | DP | Java program to find number of ways to get sum ' x ' with ' n ' dice where every dice has ' m ' faces ; The main function that returns the number of ways to get sum ' x ' with ' n ' dice and ' m ' with m faces . ; Create a table to store the results of subproblems . One extra row and column are used for simplicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for only one dice ; Fill rest of the entries in table using recursive relation i : number of dice , j : sum ; Return value ; Driver Code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static long findWays ( int m , int n , int x ) { long [ ] [ ] table = new long [ n + 1 ] [ x + 1 ] ; for ( int j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= x ; j ++ ) { for ( int k = 1 ; k < j && k <= m ; k ++ ) table [ i ] [ j ] += table [ i - 1 ] [ j - k ] ; } } return table [ n ] [ x ] ; } public static void main ( String [ ] args ) { System . out . println ( findWays ( 4 , 2 , 1 ) ) ; System . out . println ( findWays ( 2 , 2 , 3 ) ) ; System . out . println ( findWays ( 6 , 3 , 8 ) ) ; System . out . println ( findWays ( 4 , 2 , 5 ) ) ; System . out . println ( findWays ( 4 , 3 , 5 ) ) ; } }
Dice Throw | DP | * * The main function that returns number of ways to get sum ' x ' * with ' n ' dice and ' m ' with m faces . * * @ author Pedro H . Chaves < pedrohcd @ hotmail . com > < https : github . com / pedrohcdo > ; * * Count ways * * @ param f * @ param d * @ param s * @ return ; Create a table to store results of subproblems . One extra row and column are used for simpilicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for no dices If you do not have any data , then the value must be 0 , so the result is 1 ; Iterate over dices ; Iterate over sum ; The result is obtained in two ways , pin the current dice and spending 1 of the value , so we have mem [ i - 1 ] [ j - 1 ] remaining combinations , to find the remaining combinations we would have to pin the values ? ? above 1 then we use mem [ i ] [ j - 1 ] to sum all combinations that pin the remaining j - 1 's. But there is a way, when "j-f-1> = 0" we would be adding  extra combinations, so we remove the combinations that only pin the extrapolated dice face and  subtract the extrapolated combinations. ; * * Main * * @ param args
public class GFG { public static long findWays ( int f , int d , int s ) { long mem [ ] [ ] = new long [ d + 1 ] [ s + 1 ] ; mem [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= d ; i ++ ) { for ( int j = i ; j <= s ; j ++ ) { mem [ i ] [ j ] = mem [ i ] [ j - 1 ] + mem [ i - 1 ] [ j - 1 ] ; if ( j - f - 1 >= 0 ) mem [ i ] [ j ] -= mem [ i - 1 ] [ j - f - 1 ] ; } } return mem [ d ] [ s ] ; } public static void main ( String [ ] args ) { System . out . println ( findWays ( 4 , 2 , 1 ) ) ; System . out . println ( findWays ( 2 , 2 , 3 ) ) ; System . out . println ( findWays ( 6 , 3 , 8 ) ) ; System . out . println ( findWays ( 4 , 2 , 5 ) ) ; System . out . println ( findWays ( 4 , 3 , 5 ) ) ; } }
Minimum insertions to form a palindrome | DP | A Naive recursive Java program to find minimum number insertions needed to make a string palindrome ; Recursive function to find minimum number of insertions ; Base Cases ; Check if the first and last characters are same . On the basis of the comparison result , decide which subrpoblem ( s ) to call ; Driver program to test above functions
class GFG { static int findMinInsertions ( char str [ ] , int l , int h ) { if ( l > h ) return Integer . MAX_VALUE ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( Integer . min ( findMinInsertions ( str , l , h - 1 ) , findMinInsertions ( str , l + 1 , h ) ) + 1 ) ; } public static void main ( String args [ ] ) { String str = " geeks " ; System . out . println ( findMinInsertions ( str . toCharArray ( ) , 0 , str . length ( ) - 1 ) ) ; } }
Longest Palindromic Subsequence | DP | Java program of above approach ; A utility function to get max of two integers ; Returns the length of the longest palindromic subsequence in seq ; Base Case 1 : If there is only 1 character ; Base Case 2 : If there are only 2 characters and both are same ; If the first and last characters match ; If the first and last characters do not match ; Driver program to test above function
class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int lps ( char seq [ ] , int i , int j ) { if ( i == j ) { return 1 ; } if ( seq [ i ] == seq [ j ] && i + 1 == j ) { return 2 ; } if ( seq [ i ] == seq [ j ] ) { return lps ( seq , i + 1 , j - 1 ) + 2 ; } return max ( lps ( seq , i , j - 1 ) , lps ( seq , i + 1 , j ) ) ; } public static void main ( String [ ] args ) { String seq = " GEEKSFORGEEKS " ; int n = seq . length ( ) ; System . out . printf ( " The ▁ length ▁ of ▁ the ▁ LPS ▁ is ▁ % d " , lps ( seq . toCharArray ( ) , 0 , n - 1 ) ) ; } }
Smallest value in each level of Binary Tree | Java program to print smallest element in each level of binary tree . ; A Binary Tree Node ; return height of tree ; Inorder Traversal Search minimum element in each level and store it into vector array . ; height of tree for the size of vector array ; vector for store all minimum of every level ; save every level minimum using inorder traversal ; print every level minimum ; Utility function to create a new tree node ; Driver Code ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1
import java . util . Arrays ; class GFG { static int INT_MAX = ( int ) 10e6 ; static class Node { int data ; Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root , int [ ] res , int level ) { if ( root != null ) { printPerLevelMinimum ( root . left , res , level + 1 ) ; if ( root . data < res [ level ] ) res [ level ] = root . data ; printPerLevelMinimum ( root . right , res , level + 1 ) ; } } static void perLevelMinimumUtility ( Node root ) { int n = heightoftree ( root ) , i ; int [ ] res = new int [ n ] ; Arrays . fill ( res , INT_MAX ) ; printPerLevelMinimum ( root , res , 0 ) ; System . out . print ( "Every level minimum isNEW_LINE"); for ( i = 0 ; i < n ; i ++ ) { System . out . print ( " level ▁ " + i + " ▁ min ▁ is ▁ = ▁ " + res [ i ] + "NEW_LINE"); } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; perLevelMinimumUtility ( root ) ; } }
Smallest value in each level of Binary Tree | JAVA program to print minimum element in each level of binary tree . ; A Binary Tree Node ; return height of tree ; Iterative method to find every level minimum element of Binary Tree ; Base Case ; Create an empty queue for level order traversal ; push the root for Change the level ; for go level by level ; for check the level ; Get top of queue ; if node == null ( Means this is boundary between two levels ) ; here queue is empty represent no element in the actual queue ; increment level ; Reset min for next level minimum value ; get Minimum in every level ; Enqueue left child ; Enqueue right child ; Utility function to create a new tree node ; Driver code ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; q . add ( null ) ; int min = Integer . MAX_VALUE ; int level = 0 ; while ( q . isEmpty ( ) == false ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node == null ) { System . out . print ( " level ▁ " + level + " ▁ min ▁ is ▁ = ▁ " + min + "NEW_LINE"); if ( q . isEmpty ( ) ) break ; q . add ( null ) ; level ++ ; min = Integer . MAX_VALUE ; continue ; } if ( min > node . data ) min = node . data ; if ( node . left != null ) { q . add ( node . left ) ; } if ( node . right != null ) { q . add ( node . right ) ; } } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . print ( " Every ▁ Level ▁ minimum ▁ is " + "NEW_LINE"); printPerLevelMinimum ( root ) ; } }
Count occurrences of a string that can be constructed from another given string | Java implementation of the above approach ; Function to find the count ; Initialize hash for both strings ; hash the frequency of letters of str1 ; hash the frequency of letters of str2 ; Find the count of str2 constructed from str1 ; Return answer ; Driver code
class GFG { static int findCount ( String str1 , String str2 ) { int len = str1 . length ( ) ; int len2 = str2 . length ( ) ; int ans = Integer . MAX_VALUE ; int [ ] hash1 = new int [ 26 ] ; int [ ] hash2 = new int [ 26 ] ; for ( int i = 0 ; i < len ; i ++ ) hash1 [ ( int ) ( str1 . charAt ( i ) - ' a ' ) ] ++ ; for ( int i = 0 ; i < len2 ; i ++ ) hash2 [ ( int ) ( str2 . charAt ( i ) - ' a ' ) ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( hash2 [ i ] != 0 ) ans = Math . min ( ans , hash1 [ i ] / hash2 [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { String str1 = " geeksclassesatnoida " ; String str2 = " sea " ; System . out . println ( findCount ( str1 , str2 ) ) ; } }
Check if a string is the typed name of the given name | Java program to implement run length encoding ; Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver code
public class Improve { static boolean isVowel ( char c ) { String vowel = " aeiou " ; for ( int i = 0 ; i < vowel . length ( ) ; ++ i ) if ( vowel . charAt ( i ) == c ) return true ; return false ; } static boolean printRLE ( String str , String typed ) { int n = str . length ( ) , m = typed . length ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) != typed . charAt ( j ) ) return false ; if ( isVowel ( str . charAt ( i ) ) == false ) { j ++ ; continue ; } int count1 = 1 ; while ( i < n - 1 && str . charAt ( i ) == str . charAt ( i + 1 ) ) { count1 ++ ; i ++ ; } int count2 = 1 ; while ( j < m - 1 && typed . charAt ( j ) == str . charAt ( i ) ) { count2 ++ ; j ++ ; } if ( count1 > count2 ) return false ; } return true ; } public static void main ( String args [ ] ) { String name = " alex " , typed = " aaalaeex " ; if ( printRLE ( name , typed ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Get Level of a node in a Binary Tree | A tree node structure ; Helper function for getLevel ( ) . It returns level of the data if data is present in tree , otherwise returns 0. ; Returns level of given data value ; Driver code ; Constructing tree given in the above figure
class Node { int data ; Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { Node root ; int getLevelUtil ( Node node , int data , int level ) { if ( node == null ) return 0 ; if ( node . data == data ) return level ; int downlevel = getLevelUtil ( node . left , data , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; downlevel = getLevelUtil ( node . right , data , level + 1 ) ; return downlevel ; } int getLevel ( Node node , int data ) { return getLevelUtil ( node , data , 1 ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 3 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 4 ) ; for ( int x = 1 ; x <= 5 ; x ++ ) { int level = tree . getLevel ( tree . root , x ) ; if ( level != 0 ) System . out . println ( " Level ▁ of ▁ " + x + " ▁ is ▁ " + tree . getLevel ( tree . root , x ) ) ; else System . out . println ( x + " ▁ is ▁ not ▁ present ▁ in ▁ tree " ) ; } } }
Program to replace a word with asterisks in a sentence | Java program to censor a word with asterisks in a sentence ; Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code
class GFG { static String censor ( String text , String word ) { String [ ] word_list = text . split ( " \\ s + " ) ; String result = " " ; String stars = " " ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) stars += ' * ' ; int index = 0 ; for ( String i : word_list ) { if ( i . compareTo ( word ) == 0 ) word_list [ index ] = stars ; index ++ ; } for ( String i : word_list ) result += i + ' ▁ ' ; return result ; } public static void main ( String [ ] args ) { String extract = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ " + " portal ▁ for ▁ geeks . ▁ I ▁ am ▁ pursuing ▁ my ▁ " + " major ▁ in ▁ computer ▁ science . ▁ " ; String cen = " computer " ; System . out . println ( censor ( extract , cen ) ) ; } }
Finite Automata algorithm for Pattern Searching | Java program for Finite Automata Pattern searching Algorithm ; If the character c is same as next character in pattern , then simply increment state ; ns stores the result which is next state ; Start from the largest possible value and stop when you find a prefix which is also suffix ; This function builds the TF table which represents Finite Automata for a given pattern ; Prints all occurrences of pat in txt ; Process txt over FA . ; Driver code
class GFG { static int NO_OF_CHARS = 256 ; static int getNextState ( char [ ] pat , int M , int state , int x ) { if ( state < M && x == pat [ state ] ) return state + 1 ; int ns , i ; for ( ns = state ; ns > 0 ; ns -- ) { if ( pat [ ns - 1 ] == x ) { for ( i = 0 ; i < ns - 1 ; i ++ ) if ( pat [ i ] != pat [ state - ns + 1 + i ] ) break ; if ( i == ns - 1 ) return ns ; } } return 0 ; } static void computeTF ( char [ ] pat , int M , int TF [ ] [ ] ) { int state , x ; for ( state = 0 ; state <= M ; ++ state ) for ( x = 0 ; x < NO_OF_CHARS ; ++ x ) TF [ state ] [ x ] = getNextState ( pat , M , state , x ) ; } static void search ( char [ ] pat , char [ ] txt ) { int M = pat . length ; int N = txt . length ; int [ ] [ ] TF = new int [ M + 1 ] [ NO_OF_CHARS ] ; computeTF ( pat , M , TF ) ; int i , state = 0 ; for ( i = 0 ; i < N ; i ++ ) { state = TF [ state ] [ txt [ i ] ] ; if ( state == M ) System . out . println ( " Pattern ▁ found ▁ " + " at ▁ index ▁ " + ( i - M + 1 ) ) ; } } public static void main ( String [ ] args ) { char [ ] pat = " AABAACAADAABAAABAA " . toCharArray ( ) ; char [ ] txt = " AABA " . toCharArray ( ) ; search ( txt , pat ) ; } }
Longest Substring containing '1' | Java program to find length of longest substring containing '1' ; Function to find length of of longest substring containing '1' ; Count the number of contiguous 1 's ; Driver code
class GFG { static int maxlength ( String s ) { int n = s . length ( ) , i , j ; int ans = 0 ; for ( i = 0 ; i <= n - 1 ; i ++ ) { if ( s . charAt ( i ) == '1' ) { int count = 1 ; for ( j = i + 1 ; j <= n - 1 && s . charAt ( j ) == '1' ; j ++ ) count ++ ; ans = Math . max ( ans , count ) ; } } return ans ; } public static void main ( String [ ] args ) { String s = "11101110" ; System . out . println ( " Length ▁ of ▁ longest ▁ substring ▁ containing ▁ ' 1 ' ▁ = ▁ " + maxlength ( s ) ) ; } }
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 ) ; } }