How To Generate All Possible Letter Combinations (Java)

Java - Generating All Possible Combinations Of Letters

In this quick post, I will be showing you how to generate all the possible combinations of 1, 2, 3, 4, 5 etc. letter words using Java programming language. I have used BlueJ to test these program codes and they work 100% correctly. You will have to modify them according to your own requirements. The codes are displayed below. Feel free to use these codes for any purpose you want. :) :) :)

 


ONE LETTER WORDS ALL POSSIBLE COMBINATIONS GENERATOR:

Code:

public class All_Possible_Combinations_1
{
    
   public static void main(String args[])
   {
       
       for(int i=97; i<=122; i++)
       {
               System.out.println((char)i);
        }
        
    }
    
}

 

Output Example:

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

 


TWO LETTER WORDS ALL POSSIBLE COMBINATIONS GENERATOR:

Code:

public class All_Possible_Combinations_2
{
    
   public static void main(String args[])
   {
       
       for(int i=97; i<=122; i++)
       {
           for(int j=97; j<=122; j++)
           {
               System.out.print((char)i);
               System.out.println((char)j);
            }
        }
        
    }
    
}

 

Output Example:

aa
ab
ac
ad
ae
af
ag
ah
ai
aj
ak
al
am
an
ao
ap
aq
ar
as
at
au
av
aw
ax
ay
az
ba
bb
bc
bd
be
bf
bg
bh
bi
bj
bk
bl
bm
bn
bo
bp
bq
br
bs
bt
bu
bv
bw
bx
by
bz
ca
cb
cc
cd
ce
cf
cg
ch
ci
cj
ck
cl
cm
cn
co
cp
cq
cr
cs
ct
cu
cv
cw
cx
cy
cz
da
db
dc
dd
de
df
dg
dh
di
dj
dk
dl
dm
dn
do
dp
dq
dr
ds
dt
du
dv
dw
dx
dy
dz
ea
eb
ec
ed
ee
ef
eg
eh
ei
ej
ek
el
em
en
eo
ep
eq
er
es
et
eu
ev
ew
ex
ey
ez
fa
fb
fc
fd
fe
ff
fg
fh
fi
fj
fk
fl
fm
fn
fo
fp
fq
fr
fs
ft
fu
fv
fw
fx
fy
fz
ga
gb
gc
gd
ge
gf
gg
gh
gi
gj
gk
gl
gm
gn
go
gp
gq
gr
gs
gt
gu
gv
gw
gx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uq
ur
us
ut
uu
uv
uw
ux
uy
uz
va
vb
vc
vd
ve
vf
vg
vh
vi
vj
vk
vl
vm
vn
vo
vp
vq
vr
vs
vt
vu
vv
vw
vx
vy
vz
wa
wb
wc
wd
we
wf
wg
wh
wi
wj
wk
wl
wm
wn
wo
wp
wq
wr
ws
wt
wu
wv
ww
wx
wy
wz
xa
xb
xc
xd
xe
xf
xg
xh
xi
xj
xk
xl
xm
xn
xo
xp
xq
xr
xs
xt
xu
xv
xw
xx
xy
xz
ya
yb
yc
yd
ye
yf
yg
yh
yi
yj
yk
yl
ym
yn
yo
yp
yq
yr
ys
yt
yu
yv
yw
yx
yy
yz
za
zb
zc
zd
ze
zf
zg
zh
zi
zj
zk
zl
zm
zn
zo
zp
zq
zr
zs
zt
zu
zv
zw
zx
zy
zz

 


THREE LETTER WORDS ALL POSSIBLE COMBINATIONS GENERATOR:

Code:

public class All_Possible_Combinations_3
{

    public static void main(String args[])
    {

        for(int i=97; i<=122; i++)
        {
            for(int j=97; j<=122; j++)
            {
                for(int k=97; k<=122; k++)
                {
                    System.out.print((char)i);
                    System.out.print((char)j);
                    System.out.println((char)k);
                }
            }
        }

    }

}

 

Output Example:

TOO LONG TO BE DISPLAYED!

 


FOUR LETTER WORDS ALL POSSIBLE COMBINATIONS GENERATOR:

Code:

public class All_Possible_Combinations_4
{

    public static void main(String args[])
    {

        for(int i=97; i<=122; i++)
        {
            for(int j=97; j<=122; j++)
            {
                for(int k=97; k<=122; k++)
                {
                    for(int l=97; l<=122; l++)
                    {
                        System.out.print((char)i);
                        System.out.print((char)j);
                        System.out.print((char)k);
                        System.out.println((char)l);
                    }
                }
            }
        }

    }

}

 

Output Example:

TOO LONG TO BE DISPLAYED!

 


FIVE LETTER WORDS ALL POSSIBLE COMBINATIONS GENERATOR:

Code:

public class All_Possible_Combinations_5
{

    public static void main(String args[])
    {

        for(int i=97; i<=122; i++)
        {
            for(int j=97; j<=122; j++)
            {
                for(int k=97; k<=122; k++)
                {
                    for(int l=97; l<=122; l++)
                    {
                        for(int m=97; m<=122; m++)
                        {
                            System.out.print((char)i);
                            System.out.print((char)j);
                            System.out.print((char)k);
                            System.out.print((char)l);
                            System.out.println((char)m);
                        }
                    }
                }
            }
        }

    }

}

 

Output Example:

TOO LONG TO BE DISPLAYED!

 


Wrapping Up:

I hope you found what you were looking for. You can follow this pattern of Java code and generate all possible combinations of ‘n’ number of letter words. You’ll just have to make some minor changes in the code (which I am pretty sure anyone with basic programming knowledge can do). Also, most of the compilers are not able to execute this piece of code to generate all the possible combinations of more than 5 letter words. So, if you want to do that, you might want to switch to some super-duper powerful computers! :D :D :D

Comment below if you face any problems or if you want to give any suggestions. Also, don’t forget to share this post if you found it useful!

Thank you.

LINK TO THIS POST:

<a href="https://www.theadarshmehta.in/blog/generating-all-possible-letter-combinations-java/2016/02">How To Generate All Possible Letter Combinations (Java)</a>

Spread the love

6 thoughts on “How To Generate All Possible Letter Combinations (Java)

  1. Avishek

    java program that accept a word and display after removing duplicate letters. ex: java=jv

    Reply
  2. abhinav

    can you write a program on java about shooping mall or any other for project please

    Reply
      1. Rishi

        Can u do the above program without using so many for -loops and also without recursion

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *