Amazon and equivalent companies asked very common core java coding interview question i.e
write code for the Anagram Solver (Jumbled words to find meaningful word in Dictionary)
The fact is that the above question can not be solved without finding the permutation of the given string .
As the above question covers two answers in one , that why it is becoming popular in technical core java interviews of reputed companies.
Read Also : Permutation Of String in Java using Recursion : Java Source Code with Example
So let us first understand
What is Permutation of string ?
Permutation is the all possible combinations of the strings possible of any word . For example
if the string is "abs"
then all possible permutations of the string will be :
"sba"
"sab"
"bsa"
"bas"
"asb"
and last our original word : "abs"
In maths ,
you can calculate how many permutations of the word are possible , by using the formula
nPr = n! / (n-r)!
as in our example : "abs"
Here there are three characters in original word so n=3 ,and
we need to use all three characters so ,here r=3
nPr= 3! / (3-3)!
= 3! / 0!
= 6
and we can see above that only six permutations are shown for the string.
Read Also : Change String from lowercase to uppercase or uppercase to lowercase
Demo Here :
Read Also : Find all possible Combination of String in Java
Code :
write code for the Anagram Solver (Jumbled words to find meaningful word in Dictionary)
The fact is that the above question can not be solved without finding the permutation of the given string .
As the above question covers two answers in one , that why it is becoming popular in technical core java interviews of reputed companies.
Read Also : Permutation Of String in Java using Recursion : Java Source Code with Example
So let us first understand
What is Permutation of string ?
Permutation is the all possible combinations of the strings possible of any word . For example
if the string is "abs"
then all possible permutations of the string will be :
"sba"
"sab"
"bsa"
"bas"
"asb"
and last our original word : "abs"
In maths ,
you can calculate how many permutations of the word are possible , by using the formula
nPr = n! / (n-r)!
as in our example : "abs"
Here there are three characters in original word so n=3 ,and
we need to use all three characters so ,here r=3
nPr= 3! / (3-3)!
= 3! / 0!
= 6
and we can see above that only six permutations are shown for the string.
Read Also : Change String from lowercase to uppercase or uppercase to lowercase
Demo Here :
Read Also : Find all possible Combination of String in Java
Code :
import java.util.Scanner; public class Permutation { public static void main (String args[]) { System.out.println("Please enter the string whose permutations we need to show "); Scanner in = new Scanner(System.in); String original=in.nextLine(); System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("Results are :"); System.out.println(""); System.out.println(""); permute(original); } public static void permute( String input) { int inputLength = input.length(); boolean[ ] used = new boolean[ inputLength ]; StringBuffer outputString = new StringBuffer(); char[ ] in = input.toCharArray( ); doPermute ( in, outputString, used, inputLength, 0 ); } public static void doPermute ( char[ ] in, StringBuffer outputString, boolean[ ] used, int inputLength, int level) { if( level == inputLength) { System.out.println ( outputString.toString()); return; } for( int i = 0; i < inputLength; ++i ) { if( used[i] ) continue; outputString.append( in[i] ); used[i] = true; doPermute( in, outputString, used, inputLength, level + 1 ); used[i] = false; outputString.setLength( outputString.length() - 1 ); } } }