Recursive method to find all permutations of a String : Recursive Method « Class Definition « Java Tutorial






public class MainClass {
  public static void main(String args[]) {
    permuteString("", "String");
  }

  public static void permuteString(String beginningString, String endingString) {
    if (endingString.length() <= 1)
      System.out.println(beginningString + endingString);
    else
      for (int i = 0; i < endingString.length(); i++) {
        try {
          String newString = endingString.substring(0, i) + endingString.substring(i + 1);

          permuteString(beginningString + endingString.charAt(i), newString);
        } catch (StringIndexOutOfBoundsException exception) {
          exception.printStackTrace();
        }
      }
  }
}
String
Strign
Strnig
Strngi
Strgin
Strgni
Stirng
Stirgn
Stinrg
Stingr
Stigrn
Stignr
Stnrig
Stnrgi
Stnirg
Stnigr
Stngri
Stngir
Stgrin
Stgrni
Stgirn
Stginr
Stgnri
Stgnir
Srting
Srtign
Srtnig
Srtngi
Srtgin
Srtgni
Sritng
Sritgn
Srintg
Sringt
Srigtn
Srignt
Srntig
Srntgi
Srnitg
Srnigt
Srngti
Srngit
Srgtin
Srgtni
Srgitn
Srgint
Srgnti
Srgnit
Sitrng
Sitrgn
Sitnrg
Sitngr
Sitgrn
Sitgnr
Sirtng
Sirtgn
Sirntg
Sirngt
Sirgtn
Sirgnt
Sintrg
Sintgr
Sinrtg
Sinrgt
Singtr
Singrt
Sigtrn
Sigtnr
Sigrtn
Sigrnt
Signtr
Signrt
Sntrig
Sntrgi
Sntirg
Sntigr
Sntgri
Sntgir
Snrtig
Snrtgi
Snritg
Snrigt
Snrgti
Snrgit
Snitrg
Snitgr
Snirtg
Snirgt
Snigtr
Snigrt
Sngtri
Sngtir
Sngrti
Sngrit
Sngitr
Sngirt
Sgtrin
Sgtrni
Sgtirn
Sgtinr
Sgtnri
Sgtnir
Sgrtin
Sgrtni
Sgritn
Sgrint
Sgrnti
Sgrnit
Sgitrn
Sgitnr
Sgirtn
Sgirnt
Sgintr
Sginrt
Sgntri
Sgntir
Sgnrti
Sgnrit
Sgnitr
Sgnirt
tSring
tSrign
tSrnig
tSrngi
tSrgin
tSrgni
tSirng
tSirgn
tSinrg
tSingr
tSigrn
tSignr
tSnrig
tSnrgi
tSnirg
tSnigr
tSngri
tSngir
tSgrin
tSgrni
tSgirn
tSginr
tSgnri
tSgnir
trSing
trSign
trSnig
trSngi
trSgin
trSgni
triSng
triSgn
trinSg
tringS
trigSn
trignS
trnSig
trnSgi
trniSg
trnigS
trngSi
trngiS
trgSin
trgSni
trgiSn
trginS
trgnSi
trgniS
tiSrng
tiSrgn
tiSnrg
tiSngr
tiSgrn
tiSgnr
tirSng
tirSgn
tirnSg
tirngS
tirgSn
tirgnS
tinSrg
tinSgr
tinrSg
tinrgS
tingSr
tingrS
tigSrn
tigSnr
tigrSn
tigrnS
tignSr
tignrS
tnSrig
tnSrgi
tnSirg
tnSigr
tnSgri
tnSgir
tnrSig
tnrSgi
tnriSg
tnrigS
tnrgSi
tnrgiS
tniSrg
tniSgr
tnirSg
tnirgS
tnigSr
tnigrS
tngSri
tngSir
tngrSi
tngriS
tngiSr
tngirS
tgSrin
tgSrni
tgSirn
tgSinr
tgSnri
tgSnir
tgrSin
tgrSni
tgriSn
tgrinS
tgrnSi
tgrniS
tgiSrn
tgiSnr
tgirSn
tgirnS
tginSr
tginrS
tgnSri
tgnSir
tgnrSi
tgnriS
tgniSr
tgnirS
rSting
rStign
rStnig
rStngi
rStgin
rStgni
rSitng
rSitgn
rSintg
rSingt
rSigtn
rSignt
rSntig
rSntgi
rSnitg
rSnigt
rSngti
rSngit
rSgtin
rSgtni
rSgitn
rSgint
rSgnti
rSgnit
rtSing
rtSign
rtSnig
rtSngi
rtSgin
rtSgni
rtiSng
rtiSgn
rtinSg
rtingS
rtigSn
rtignS
rtnSig
rtnSgi
rtniSg
rtnigS
rtngSi
rtngiS
rtgSin
rtgSni
rtgiSn
rtginS
rtgnSi
rtgniS
riStng
riStgn
riSntg
riSngt
riSgtn
riSgnt
ritSng
ritSgn
ritnSg
ritngS
ritgSn
ritgnS
rinStg
rinSgt
rintSg
rintgS
ringSt
ringtS
rigStn
rigSnt
rigtSn
rigtnS
rignSt
rigntS
rnStig
rnStgi
rnSitg
rnSigt
rnSgti
rnSgit
rntSig
rntSgi
rntiSg
rntigS
rntgSi
rntgiS
rniStg
rniSgt
rnitSg
rnitgS
rnigSt
rnigtS
rngSti
rngSit
rngtSi
rngtiS
rngiSt
rngitS
rgStin
rgStni
rgSitn
rgSint
rgSnti
rgSnit
rgtSin
rgtSni
rgtiSn
rgtinS
rgtnSi
rgtniS
rgiStn
rgiSnt
rgitSn
rgitnS
rginSt
rgintS
rgnSti
rgnSit
rgntSi
rgntiS
rgniSt
rgnitS
iStrng
iStrgn
iStnrg
iStngr
iStgrn
iStgnr
iSrtng
iSrtgn
iSrntg
iSrngt
iSrgtn
iSrgnt
iSntrg
iSntgr
iSnrtg
iSnrgt
iSngtr
iSngrt
iSgtrn
iSgtnr
iSgrtn
iSgrnt
iSgntr
iSgnrt
itSrng
itSrgn
itSnrg
itSngr
itSgrn
itSgnr
itrSng
itrSgn
itrnSg
itrngS
itrgSn
itrgnS
itnSrg
itnSgr
itnrSg
itnrgS
itngSr
itngrS
itgSrn
itgSnr
itgrSn
itgrnS
itgnSr
itgnrS
irStng
irStgn
irSntg
irSngt
irSgtn
irSgnt
irtSng
irtSgn
irtnSg
irtngS
irtgSn
irtgnS
irnStg
irnSgt
irntSg
irntgS
irngSt
irngtS
irgStn
irgSnt
irgtSn
irgtnS
irgnSt
irgntS
inStrg
inStgr
inSrtg
inSrgt
inSgtr
inSgrt
intSrg
intSgr
intrSg
intrgS
intgSr
intgrS
inrStg
inrSgt
inrtSg
inrtgS
inrgSt
inrgtS
ingStr
ingSrt
ingtSr
ingtrS
ingrSt
ingrtS
igStrn
igStnr
igSrtn
igSrnt
igSntr
igSnrt
igtSrn
igtSnr
igtrSn
igtrnS
igtnSr
igtnrS
igrStn
igrSnt
igrtSn
igrtnS
igrnSt
igrntS
ignStr
ignSrt
igntSr
igntrS
ignrSt
ignrtS
nStrig
nStrgi
nStirg
nStigr
nStgri
nStgir
nSrtig
nSrtgi
nSritg
nSrigt
nSrgti
nSrgit
nSitrg
nSitgr
nSirtg
nSirgt
nSigtr
nSigrt
nSgtri
nSgtir
nSgrti
nSgrit
nSgitr
nSgirt
ntSrig
ntSrgi
ntSirg
ntSigr
ntSgri
ntSgir
ntrSig
ntrSgi
ntriSg
ntrigS
ntrgSi
ntrgiS
ntiSrg
ntiSgr
ntirSg
ntirgS
ntigSr
ntigrS
ntgSri
ntgSir
ntgrSi
ntgriS
ntgiSr
ntgirS
nrStig
nrStgi
nrSitg
nrSigt
nrSgti
nrSgit
nrtSig
nrtSgi
nrtiSg
nrtigS
nrtgSi
nrtgiS
nriStg
nriSgt
nritSg
nritgS
nrigSt
nrigtS
nrgSti
nrgSit
nrgtSi
nrgtiS
nrgiSt
nrgitS
niStrg
niStgr
niSrtg
niSrgt
niSgtr
niSgrt
nitSrg
nitSgr
nitrSg
nitrgS
nitgSr
nitgrS
nirStg
nirSgt
nirtSg
nirtgS
nirgSt
nirgtS
nigStr
nigSrt
nigtSr
nigtrS
nigrSt
nigrtS
ngStri
ngStir
ngSrti
ngSrit
ngSitr
ngSirt
ngtSri
ngtSir
ngtrSi
ngtriS
ngtiSr
ngtirS
ngrSti
ngrSit
ngrtSi
ngrtiS
ngriSt
ngritS
ngiStr
ngiSrt
ngitSr
ngitrS
ngirSt
ngirtS
gStrin
gStrni
gStirn
gStinr
gStnri
gStnir
gSrtin
gSrtni
gSritn
gSrint
gSrnti
gSrnit
gSitrn
gSitnr
gSirtn
gSirnt
gSintr
gSinrt
gSntri
gSntir
gSnrti
gSnrit
gSnitr
gSnirt
gtSrin
gtSrni
gtSirn
gtSinr
gtSnri
gtSnir
gtrSin
gtrSni
gtriSn
gtrinS
gtrnSi
gtrniS
gtiSrn
gtiSnr
gtirSn
gtirnS
gtinSr
gtinrS
gtnSri
gtnSir
gtnrSi
gtnriS
gtniSr
gtnirS
grStin
grStni
grSitn
grSint
grSnti
grSnit
grtSin
grtSni
grtiSn
grtinS
grtnSi
grtniS
griStn
griSnt
gritSn
gritnS
grinSt
grintS
grnSti
grnSit
grntSi
grntiS
grniSt
grnitS
giStrn
giStnr
giSrtn
giSrnt
giSntr
giSnrt
gitSrn
gitSnr
gitrSn
gitrnS
gitnSr
gitnrS
girStn
girSnt
girtSn
girtnS
girnSt
girntS
ginStr
ginSrt
gintSr
gintrS
ginrSt
ginrtS
gnStri
gnStir
gnSrti
gnSrit
gnSitr
gnSirt
gntSri
gntSir
gntrSi
gntriS
gntiSr
gntirS
gnrSti
gnrSit
gnrtSi
gnrtiS
gnriSt
gnritS
gniStr
gniSrt
gnitSr
gnitrS
gnirSt
gnirtS








5.10.Recursive Method
5.10.1.Recursion: a method (function) calls itself
5.10.2.The Towers of Hanoi
5.10.3.Recursion: another example
5.10.4.Recursive factorial method
5.10.5.Recursive fibonacci method
5.10.6.Recursive method to find all permutations of a String