JAVA PROGRAM TO PRINT ANAGRAMS WITHOUT USING RECURSION
/*Computer Generated Outputs:
*/
import java.io.*;
class anagram
{
static boolean unique(int n,int s)
{ int nn=n;
while(nn>0)
{int d=nn%10;
if(d>s-1||d==0)
break;
nn=nn/10;
}
if(nn>0)
return false;
boolean f=true;String a=""+n;
for(int i=0;i<s-1;i++)
{nn=n;int p=Integer.parseInt(""+a.charAt(i));
for(int j=i+1;j<(s-1);j++)
{int d=Integer.parseInt(""+ a.charAt(j));
if(d==p)
{f=false;break;}
nn=nn/10;
}
}
return (f);
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print(" Enter String : ");
String s=br.readLine();
int len=s.length()+1;System.out.println();
if(len>10)
{System.out.println("INVALID INPUT Sorry!!!!!!");System.exit(0);
}
char A[]=new char[len];String aa="",bb="";
for(int i=1;i<len;i++)
{A[i]=s.charAt(i-1);aa=aa+i;bb=i+bb;
}
int cn=0;long as=0;
int a=Integer.parseInt(aa);int b=Integer.parseInt(bb);
for(int i=a;i<=b;i++)
{
if(unique(i,len))
{ String x=""+i;cn++;as++;
for(int j=0;j<x.length();j++)
{
System.out.print(A[Integer.parseInt(""+x.charAt(j))]);
}
if(cn==10)
{cn=0;System.out.println();}
else
System.out.print(" ");
}
}
System.out.println("\nTotal number of Anagrams = "+as);}
}
/*
OUTPUTS ARE :
OUTPUT 1:
Enter String : TOP
TOP TPO OTP OPT PTO POT
Total number of Anagrams = 6
OUTPUT 2:
Enter String : ABHI
ABHI ABIH AHBI AHIB AIBH AIHB BAHI BAIH BHAI BHIA
BIAH BIHA HABI HAIB HBAI HBIA HIAB HIBA IABH IAHB
IBAH IBHA IHAB IHBA
Total number of Anagrams = 24
OUTPUT 3:
Enter String : ABCDE
ABCDE ABCED ABDCE ABDEC ABECD ABEDC ACBDE ACBED ACDBE ACDEB
ACEBD ACEDB ADBCE ADBEC ADCBE ADCEB ADEBC ADECB AEBCD AEBDC
AECBD AECDB AEDBC AEDCB BACDE BACED BADCE BADEC BAECD BAEDC
BCADE BCAED BCDAE BCDEA BCEAD BCEDA BDACE BDAEC BDCAE BDCEA
BDEAC BDECA BEACD BEADC BECAD BECDA BEDAC BEDCA CABDE CABED
CADBE CADEB CAEBD CAEDB CBADE CBAED CBDAE CBDEA CBEAD CBEDA
CDABE CDAEB CDBAE CDBEA CDEAB CDEBA CEABD CEADB CEBAD CEBDA
CEDAB CEDBA DABCE DABEC DACBE DACEB DAEBC DAECB DBACE DBAEC
DBCAE DBCEA DBEAC DBECA DCABE DCAEB DCBAE DCBEA DCEAB DCEBA
DEABC DEACB DEBAC DEBCA DECAB DECBA EABCD EABDC EACBD EACDB
EADBC EADCB EBACD EBADC EBCAD EBCDA EBDAC EBDCA ECABD ECADB
ECBAD ECBDA ECDAB ECDBA EDABC EDACB EDBAC EDBCA EDCAB EDCBA
Total number of Anagrams = 120
OUTPUT 4:
Enter String : ABCDEFGHIJKL
INVALID INPUT Sorry!!!!!!
OUTPUT 5:
Enter String : 12345
12345 12354 12435 12453 12534 12543 13245 13254 13425 13452
13524 13542 14235 14253 14325 14352 14523 14532 15234 15243
15324 15342 15423 15432 21345 21354 21435 21453 21534 21543
23145 23154 23415 23451 23514 23541 24135 24153 24315 24351
24513 24531 25134 25143 25314 25341 25413 25431 31245 31254
31425 31452 31524 31542 32145 32154 32415 32451 32514 32541
34125 34152 34215 34251 34512 34521 35124 35142 35214 35241
35412 35421 41235 41253 41325 41352 41523 41532 42135 42153
42315 42351 42513 42531 43125 43152 43215 43251 43512 43521
45123 45132 45213 45231 45312 45321 51234 51243 51324 51342
51423 51432 52134 52143 52314 52341 52413 52431 53124 53142
53214 53241 53412 53421 54123 54132 54213 54231 54312 54321
Total number of Anagrams = 120
*/
Comments
Post a Comment