我已经能够从字符串中生成长度为4的所有排列,我现在面临的问题是所有排列都有重复项,如{abcd),{adbc},{bcda} ...都是与集合的数学定义相同。
java-8中是否有一个简短版本,我可以得到纯集?
生成所有排列的我的代码:
static Set<String> setA = new HashSet<>();
public static Set<String> permutation(String prefix, String str, int len) {
int n = str.length();
if (prefix.length() == len) {
setA.add(prefix);
} else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n), len);
}
}
return setA;
}
public static void main(String[] args) {
System.out.println(permutation("", "ABCDEFGH", 4));
}
现在输出:
[DHEA, BFGA, DHEB, DHEC, DHEF, DHEG, CEAH, CEAG, CEAF, CEAD, CEAB, DHFA, BFHC, ACEB, BFHA, CEBH, ACDH, CEBG, CEBF, ACDF, BFGH, ACDG, CEBD, ACDE, BFGE, ACDB, BFGD, CEBA, BFGC, ACFB, CEDA, DHGC, EGBA, DHGE, DHGF, EGBC, EGBD, EGBF, EGBH, ACEG, ACEH, BFHG, ACEF, BFHE, ACED, BFHD, DHFB, ACGD, DHFC, ACGB, DHFE, EGAB, EGAC, DHFG, EGAD, EGAF, EGAH, CEDH, ACFH, CEDG, CEDF, ACFG, ACFD, DHGA, ACFE, CEDB, DHGB, ACHD, EGDB, ACHE, CEFB, EGDC, ACHB, CEFA, EGDF, EGDH, ACGH, ACGE, ACGF, CEGD, EGCA, EGCB, CEGB, CEGA, EGCD, EGCF, EGCH, CEFH, CEFG, ACHF, ACHG, CEFD, EGDA, EGFD, CEHD, CEHB, CEHA, EGFH, CEGH, CEGF, EGFA, CEHG, EGFB, CEHF, EGFC, EGHF, ECAB, CABH, EGHA, CABG, EGHB, CABF, EGHC, CABE, EGHD, CABD, CADE, ECBD, ECBF, CADB, ECBG, ECBH, GEAB, CAEF, ECAD, CAED, ECAF, CAEB, ECAG, ECAH, CADH, ECBA, CADG, CADF, CAFG, ECDF, ECDG, GEBD, CAFE, ECDH, CAFD, GEBF, CAFB, GEBH, GECB, GECA, CAEH, GECD, CAEG, CAGH, GEAD, GEAC, CAGF, GEAF, CAGE, CAGD, GEAH, CAGB, ECDA, ECDB, GEBA, GEBC, CAFH, ECFH, FBAE, GEDF, CAHG, FBAG, CAHF, FBAH, GEDH, CAHE, CAHD, CAHB, ECGA, ECGB, ECGD, ECGF, ECGH, GECF, GECH, ECFA, ECFB, GEDA, ECFD, GEDC, GEDB, FBAC, ECFG, FBAD, FBCG, HDAF, FBCH, GEFH, HDAE, HDAG, FBDA, HDBA, FBDC, HDBC, FBDE, HDBE, FBDG, ECHA, ECHB, GEFA, ECHD, FBCA, GEFC, GEFB, ECHF, HDAB, ECHG, FBCD, GEFD, FBCE, HDAC, HDCG, FBDH, HDBG, HDBF, GEHA, FBEA, GEHC, GEHB, FBEC, HDCB, FBED, GEHD, HDCA, GEHF, FBEG, HDCF, FBEH, HDCE, GABH, FBHA, HDFA, FBHC, FBHD, GACB, HDFC, FBHE, HDFB, GACD, HDFE, FBHG, GACF, HDFG, GACE, GACH, AHBG, AHBE, AHBF, AHBC, FBGA, AHBD, FBGC, HDEB, FBGD, HDEA, FBGE, GABC, HDEC, GABE, HDEF, FBGH, GABD, GABF, HDEG, AHCF, AHCG, AHCD, GAEB, AHCE, AHCB, GAED, GAEC, GAEF, GAEH, AHDG, AHDE, HDGB, AHDF, HDGA, AHDC, GADC, GADB, HDGC, GADE, HDGF, AHDB, HDGE, GADF, GADH, AHEF, AHEG, AHED, AHEB, AHEC, BGAH, BGAF, AHFG, BGAE, GAFC, BGAD, GAFB, AHFE, BGAC, GAFE, GAFD, AHFC, AHFD, AHFB, GAFH, AHGF, AHGD, AHGE, AHGB, AHGC, GAHC, BGCH, GAHB, GAHE, BGCF, GAHD, BGCE, BGCD, GAHF, BGCA, BGDH, BGDF, BGDE, BGDC, BGDA, ADBG, ADBH, BGEH, ADBE, ADBF, BGEF, ADBC, BGED, BGEC, BGEA, CFAH, ADCH, CFAG, ADCF, BGFH, CFAE, ADCG, CFAD, ADCE, BGFE, CFAB, ADCB, BGFD, BGFC, BGFA, BGHA, CFBH, CFBG, CFBE, CFBD, CFBA, ADFB, EHBA, EHBC, EHBD, EHBF, EHBG, ADEH, ADEF, ADEG, BGHF, BGHE, ADEB, BGHD, ADEC, BGHC, ADGB, CFEA, ADGC, EHAB, EHAC, EHAD, EHAF, EHAG, BCAH, BCAG, CFDH, BCAF, CFDG, ADFG, BCAE, ADFH, BCAD, CFDE, ADFE, ADFC, CFDB, CFDA, ADHC, EHDA, EHDB, EHDC, ADHB, EHDF, EHDG, CFEH, ADGH, CFEG, ADGF, CFED, ADGE, CFEB, BCDA, CFGB, EHCA, CFGA, EHCB, EHCD, EHCF, EHCG, DEAH, DEAG, DEAF, ADHG, DEAC, DEAB, ADHE, ADHF, CFHD, DECA, EHFC, EHFD, BCEA, CFHB, FGAB, CFHA, FGAC, EHFG, FGAD, FGAE, FGAH, DEBH, DEBG, BCDH, DEBF, BCDG, CFGH, BCDF, BCDE, DEBC, EHGA, CFGE, EHGB, CFGD, DEBA, EHGC, BCFD, BCFA, DECH, DECG, BCEH, DECF, BCEG, BCEF, CFHG, EHFA, BCED, CFHE, DECB, EHFB, BCGE, FGCB, BCGD, FGCD, FGCE, BCGA, FGCH, CBAH, CBAG, CBAF, BCFH, CBAE, BCFG, CBAD, FGDA, BCFE, FGDB, BCHF, EHGD, FGBA, BCHE, DEFC, BCHD, DEFB, EHGF, FGBC, DEFA, FGBD, FGBE, BCHA, FGBH, BCGH, BCGF, FGCA, EDBC, FGED, DEGC, CBDA, DEGB, EDBF, DEGA, EDBG, FGEH, EDBH, DEFH, DEFG, EDCA, EDCB, BCHG, DEHF, EDAB, FGDC, CBED, EDAC, FGDE, DEHC, CBEA, DEHB, EDAF, DEHA, EDAG, FGDH, EDAH, CBDH, CBDG, DEGH, FGEA, CBDF, EDBA, FGEB, CBDE, DEGF, FGEC, GFBD, CBFE, GFBC, CBFD, GFBE, GFBH, CBFA, FGHA, FGHB, FGHC, GFCA, CBEH, FGHD, CBEG, FGHE, CBEF, DEHG, GFCB, GFAC, CBGF, DABC, GFAB, CBGE, EDCF, GFAE, CBGD, EDCG, GFAD, EDCH, CBGA, GFAH, CBFH, CBFG, GFBA, DACE, EDFG, FCAD, CBHG, EDFH, FCAE, GFDE, CBHF, GFDH, CBHE, DACB, FCAG, CBHD, FCAH, CBHA, EDGA, EDGB, GFEA, EDGC, DABH, FCBA, GFEC, DABG, GFEB, DABF, EDGF, CBGH, DABE, FCBD, GFED, GFCE, GFCD, GFCH, EDFA, EDFB, EDFC, GFDB, DACH, GFDA, DACG, FCAB, DACF, GFDC, DAEG, DAEF, HEAD, HEAG, HEAF, DAEC, DAEB, FCDA, FCDB, HEBA, FCDE, HEBD, HEBC, DAFH, EDGH, FCBE, DAFG, FCBG, DAFE, FCBH, GFEH, DAFC, DAFB, EDHA, EDHB, EDHC, EDHF, EDHG, HEAC, DAEH, HEAB, FCEH, HECG, DAGH, HECF, DAGF, DAGE, DAGC, DAGB, HEDB, HEDA, GBAC, HEDC, GBAE, HEDF, GBAD, FCDG, HEBF, FCDH, DAHG, HEBG, DAHF, DAHE, DAHC, DAHB, GFHB, FCEA, GFHA, FCEB, GFHD, HECA, GFHC, FCED, GFHE, HECB, FCEG, HECD, FCHA, FCHB, GBCA, HEFB, FCHD, HEFA, FCHE, HEFD, HEFC, FCHG, GBCE, GBCD, GBCF, HEFG, GBAF, HEDG, GBAH, FCGA, FCGB, FCGD, FCGE, FCGH, GBEA, GBEC, GBED, GBEF, GBEH, GBCH, HEGA, HEGC, GBDA, HEGB, GBDC, HEGD, GBDF, GBDE, HEGF, GBDH, HABD, HABC, HABF, HABE, HABG, BHAG, BHAF, BHAE, BHAD, BHAC, GBFA, GBFD, GBFC, GBFE, GBFH, HADB, HADC, HADF, HADE, HADG, BHCG, GBHA, BHCF, GBHD, BHCE, GBHC, BHCD, GBHF, GBHE, HACB, HACE, BHCA, HACD, HACG, HACF, HAFB, BHDG, HAFD, BHDF, HAFC, BHDE, HAFE, BHDC, HAFG, BHDA, AEBH, AEBF, HAEC, AEBG, BHEG, HAEB, AEBD, BHEF, HAED, BHED, HAEG, AEBC, BHEC, HAEF, BHEA, CGAH, AECG, CGAF, AECH, CGAE, BHFG, CGAD, AECF, BHFE, CGAB, AECD, BHFD, BHFC, AECB, BHFA, HAGC, CGBH, HAGB, AEDH, HAGE, CGBF, HAGD, AEDF, CGBE, AEDG, CGBD, HAGF, BHGF, BHGE, AEDB, BHGD, CGBA, AEDC, BHGC, BHGA, AEGB, BDAH, BDAG, CGDH, AEFH, BDAF, BDAE, CGDF, CGDE, AEFG, BDAC, AEFD, CGDB, AEFB, CGDA, AEFC, AEHB, CGFA, AEHC, CGEH, CGEF, AEGH, CGED, AEGF, AEGC, CGEB, AEGD, CGEA, DFAH, DFAG, BDCH, BDCG, CGFH, DFAE, BDCF, BDCE, DFAC, AEHF, CGFE, DFAB, AEHG, CGFD, AEHD, BDCA, CGFB, BDEA, CGHB, CGHA, FHAB, FHAC, FHAD, FHAE, FHAG, DFBH, DFBG, DFBE, DFBC, DFBA, BDFC, BDFA, DFCH, DFCG, BDEH, BDEG, DFCE, BDEF, CGHF, CGHE, DFCB, BDEC, CGHD, DFCA, DFEB, FHCA, BDGC, DFEA, FHCB, BDGA, FHCD, FHCE, FHCG, BDFH, BDFG, BDFE, FHDA, BDHE, FHBA, BDHC, FHBC, BDHA, FHBD, FHBE, FHBG, DFEH, DFEG, BDGH, BDGF, BDGE, DFEC, FHEC, DFGC, FHED, DFGB, DFGA, FHEG, BDHG, BDHF, DFHE, FHDB, FHDC, DFHC, DFHB, FHDE, DFHA, FHDG, DFGH, FHEA, DFGE, FHEB, FHGE, DFHG, DBAH, DBAG, DBAF, FHGA, DBAE, FHGB, FHGC, DBAC, FHGD, FDAC, FDAE, DBCA, FDAG, FDAH, FDBA, FDBC, DBCH, DBCG, DBCF, DBCE, FDAB, DBEF, FDCE, HFAD, HFAC, FDCG, DBEC, FDCH, HFAE, DBEA, HFAG, HFBA, HFBC, DBFG, FDBE, DBFE, FDBG, DBFC, FDBH, DBFA, FDCA, FDCB, DBEH, HFAB, DBEG, DBGH, EABF, FDEG, EABG, FDEH, HFCE, DBGF, EABH, DBGE, HFCG, DBGC, DBGA, HFDA, EACB, GCAB, HFDC, EACD, HFDB, GCAD, HFDE, DBFH, EACF, HFBE, HFBD, DBHG, HFBG, DBHF, DBHE, DBHC, DBHA, FDEA, FDEB, FDEC, HFCB, EABC, HFCA, EABD, HFCD, EADH, GCBF, HFEG, GCBH, FDHA, FDHB, FDHC, FDHE, FDHG, EACG, GCAF, HFDG, EACH, GCAE, GCAH, FDGA, FDGB, EADB, FDGC, GCBA, HFEB, EADC, HFEA, FDGE, HFED, HFEC, EADF, GCBE, EADG, FDGH, GCBD, GCDH, EAGB, EAGC, GCEB, EAGD, GCEA, GCED, EAGF, GCEF, EAGH, GCEH, EAFB, GCDA, HFGB, EAFC, HFGA, EAFD, HFGD, GCDB, HFGC, GCDE, EAFG, HFGE, EAFH, GCDF, HBAG, EAHB, GCFA, EAHC, EAHD, GCFB, EAHF, GCFE, EAHG, GCFD, HBAD, HBAC, HBAF, GCFH, HBAE, HBDA, HBDC, HBDE, HBDG, HBDF, GCHA, GCHB, GCHE, GCHD, HBCA, HBCD, GCHF, HBCF, HBCE, HBCG, HBFA, HBFC, HBFE, HBFD, HBFG, AFBG, AFBH, AFBE, HBEA, AFBC, HBED, AFBD, HBEC, HBEF, HBEG, AFCH, CHAG, CHAF, CHAE, AFCG, CHAD, AFCD, AFCE, CHAB, AFCB, CHBG, HBGA, AFDG, CHBF, HBGD, AFDH, CHBE, HBGC, AFDE, CHBD, HBGF, HBGE, AFDC, CHBA, AFDB, AFEH, AFEG, AFED, AFEB, AFEC, BEAH, BEAG, BEAF, CHDG, CHDF, BEAD, CHDE, BEAC, CHDB, CHDA, AFHB, AFGH, CHEG, CHEF, CHED, AFGD, AFGE, CHEB, AFGB, CHEA, AFGC, CHGA, DGAH, BECH, DGAF, BECG, DGAE, BECF, CHFG, AFHG, DGAC, BECD, CHFE, DGAB, AFHE, CHFD, AFHC, BECA, CHFB, AFHD, CHFA, DGBH, BEDH, DGBF, BEDG, DGBE, BEDF, CHGF, DGBC, CHGE, BEDC, CHGD, DGBA, BEDA, CHGB, BEFA, DGCH, DGCF, DGCE, DGCB, DGCA, BEGC, DGEA, BEGA, CDAH, ABCH, CDAG, CDAF, ABCF, BEFH, CDAE, ABCG, BEFG, ABCD, ABCE, CDAB, BEFD, BEFC, BEHD, DGFB, ABEC, BEHC, DGFA, BEHA, CDBH, CDBG, DGEH, ABDG, CDBF, ABDH, BEGH, CDBE, DGEF, ABDE, ABDF, BEGF, ABDC, DGEC, BEGD, CDBA, DGEB, ABFC, EFBA, ABFD, EFBC, EFBD, EFBG, EFBH, ABEH, DGFH, ABEF, ABEG, BEHG, DGFE, ABED, BEHF, DGFC, EFCA, ABGD, ABGE, CDEB, DGHC, CDEA, DGHB, EFAB, ABGC, DGHA, EFAC, EFAD, EFAG, EFAH, ABFG, ABFH, ABFE, ABHE, EFDC, ABHF, GHBA, ABHC, CDFB, GHBD, ABHD, CDFA, GHBC, EFDG, GHBF, EFDH, GHBE, CDEH, ABGH, CDEG, CDEF, ABGF, DGHF, GHCA, DGHE, BADC, CDGE, EFCB, DCBA, EFCD, GHAC, CDGB, GHAB, CDGA, GHAE, EFCG, GHAD, EFCH, GHAF, DCAH, BACH, DCAG, DCAF, BACF, CDFH, DCAE, BACG, CDFG, ABHG, BACD, EFDA, BACE, CDFE, DCAB, EFDB, BAED, CDHF, FEAB, CDHE, FEAC, GHDC, FEAD, GHDF, BAEC, GHDE, CDHB, CDHA, FEAG, FEAH, DCBH, DCBG, EFGA, BADG, DCBF, EFGB, GHEA, BADH, CDGH, DCBE, EFGC, BADE, EFGD, FEBA, GHEC, BADF, CDGF, GHEB, BAFE, GHCB, BAFC, GHCE, BAFD, GHCD, GHCF, BAEH, BAEF, GHDB, BAEG, CDHG, GHDA, BAGF, EFHG, FECD, HGAC, GHFE, HGAB, BAGD, HGAE, BAGE, DCEB, FECG, HGAD, DCEA, FECH, BAGC, HGAF, FEDA, FEDB, BAFG, FEDC, BAFH, EBAC, HGBA, BAHG, FEBC, DCFE, FEBD, GHED, BAHE, EFGH, BAHF, GHEF, BAHC, DCFB, FEBG, BAHD, DCFA, FEBH, EFHA, EFHB, EFHC, GHFB, DCEH, EFHD, FECA, GHFA, BAGH, DCEG, FECB, GHFD, DCEF, GHFC, HGCE, DCGF, HGCD, DCGE, HGCF, DCGB, DCGA, EBCA, HGDB, HGDA, DCFH, EBCD, GDAC, DCFG, GDAB, HGDC, EBAD, HGBD, DCHG, HGBC, DCHF, EBAF, FEDG, HGBF, DCHE, EBAG, FEDH, HGBE, EBAH, DCHB, DCHA, HGCA, DCGH, HGCB, EBDG, FEGH, GDBF, EBDH, GDBE, HGEF, GDBH, FEHA, FEHB, FEHC, GDCA, HGFB, FEHD, HGFA, HGFD, GDCB, HGFC, FEHG, GDCE, HGFE, EBCF, GDAE, HGDF, EBCG, HGDE, EBCH, GDAF, GDAH, FEGA, EBDA, FEGB, HGEA, FEGC, EBDC, FEGD, HGEC, GDBA, HGEB, EBDF, GDBC, HGED, EBGA, GDEA, EBGC, EBGD, GDEC, GDEB, EBGF, FABC, FABD, EBGH, FABE, GDEF, GDCF, GDCH, EBFA, EBFC, EBFD, EBFG, EBFH, FACH, HCAG, HCAF, FADB, FADC, HCBA, FADE, HCBD, FADG, HCBF, FADH, HCBE, FABG, FABH, GDEH, EBHA, EBHC, GDFB, EBHD, GDFA, FACB, EBHF, GDFC, EBHG, FACD, FACE, GDFE, HCAB, GDFH, HCAE, FACG, HCAD, HCDB, HCDA, HCDF, HCDE, HCDG, HCBG, GDHB, GDHA, FAEB, FAEC, GDHC, FAED, GDHF, GDHE, FAEG, FAEH, FAHB, FAHC, HCFB, FAHD, HCFA, FAHE, HCFD, FAHG, HCFE, HCFG, AGBH, AGBF, AGBD, FAGB, HCEA, AGBE, FAGC, FAGD, AGBC, FAGE, HCEB, HCED, FAGH, HCEG, HCEF, AGCH, AGCE, AGCF, AGCD, AGCB, AGDH, HCGA, AGDF, HCGB, HCGE, AGDE, HCGD, AGDB, AGDC, HCGF, AGEH, AGEF, AGEC, AGED, AGEB, BFAH, BFAG, AGFH, BFAE, BFAD, BFAC, AGFD, AGFE, AGFB, AGFC, DHAG, BFCH, DHAF, BFCG, DHAE, BFCE, DHAC, AGHF, BFCD, DHAB, AGHD, AGHE, BFCA, AGHB, AGHC, DHBG, BFDH, DHBF, BFDG, DHBE, BFDE, DHBC, BFDC, DHBA, BFDA, ACBH, DHCG, ACBF, BFEH, DHCF, ACBG, BFEG, DHCE, ACBD, ACBE, BFED, DHCB, BFEC, DHCA, BFEA]
期望:
[ADEH, BCDH, ABEH, ADEF, BDFH, ADEG, AEGH, BCDG, CFGH, DEFH, BDFG, ABEF, BCDF, BCDE, CDGH, ABEG, DEFG, ACGH, ACDH, ACDF, BFGH, ADFG, ADFH, BCEH, ACDG, CEFH, ABFG, BCEG, BDGH, CEFG, DEGH, ACDE, ABFH, BCEF, ABCH, AFGH, ACEG, ABCF, BEFH, ADGH, CDEH, ACEH, BEFG, ABCG, BCFH, ABCD, CDEG, CEGH, ABGH, ACEF, ABCE, BCFG, CDEF, EFGH, AEFH, BDEH, ACFH, ABDG, BDEG, BEGH, AEFG, ABDH, DFGH, CDFH, BDEF, ABDE, ACFG, ABDF, BCGH, CDFG]
如果要查找唯一的单词(如前所述,不考虑字符顺序,可以在完成排列后再执行一个步骤。
Set<String> setA = new HashSet<>();
setA.add("ABCD");
setA.add("ABDC");
Set<String> sorted = setA.stream()
.map(s -> {
char[] chars = s.toCharArray();
Arrays.sort(chars);
return new String(chars);
})
.collect(Collectors.toSet());
sorted.forEach(s -> System.out.println(s));
输出
ABCD