对于6x6字符矩阵,或者实际上是6个字符串的数组,我需要找到所有可能的六字符串,这些字符串可以沿主对角线(\)形成。
所以对于矩阵
[[a[0], a[1], a[2], a[3], a[4], a[5]]
[b[0], b[1], b[2], b[3], b[4], b[5]]
[c[0], c[1], c[2], c[3], c[4], c[5]]
[d[0], d[1], d[2], d[3], d[4], d[5]]
[e[0], e[1], e[2], e[3], e[4], e[5]]
[f[0], f[1], f[2], f[3], f[4], f[5]]]
其中每个a0,b1,c2,...是一个字符,我需要打印所有可能的字符串,这些字符串来自于从左上角到右下角沿对角线组合字符,例如“a [0] + b [1] + c [2] + d [3] + e [4] + f [5]”。
不同的组合来自矩阵中不同的字符串位置,如[a,b,c,d,e,f],[b,a,c,d,e,f],[f,d, e,b,a,c]等。据我所知,有720个可能的输出字符串(6!)。
我只知道每个Java和Python的一点点,但我觉得我可以用for循环做一个非常简单的解决方案。有什么想法吗?
答案 0 :(得分:0)
假设数组中的每个字符串与字符串数组具有完全相同的字符数,那么提取对角字符只是循环的问题
sixStrings[index].charAt(index);
由于您的对角线要求,您需要跟踪的唯一计数器是单个索引。该索引同时用作字符串索引和子char索引。只需将每个放入一个长度相同的新char
数组(并在同一索引处)。
就查找这六个字符的所有可能排列而言,请参阅此问题,该问题可以回答语言对抗性问题,也可以回答Java问题: Generate list of all possible permutations of a string
答案 1 :(得分:0)
使用递归可以非常轻松地完成。
import java.util.ArrayList;
import java.util.List;
public class JavaApplication120 {
public static void main(String[] args) {
String[] array = {"asfghl", "qwndof", "zzetil", "bbbdie", "apqith", "nzivhe"};
String diagonal = "";
for (int i = 0; i < array.length; i++) {
diagonal += array[i].charAt(i);
}
List<String> permutations = allPermutation("", diagonal);
System.out.println("size is : " + permutations.size());
System.out.println(permutations);
}
public static List<String> allPermutation(String permutation, String letters) {
List<String> list = new ArrayList<>();
if (letters.isEmpty()) {
list.add(permutation);
return list;
}
for (int i = 0; i < letters.length(); i++) {
list.addAll(allPermutation(permutation + letters.charAt(i), removeCharAt(i, letters)));
}
return list;
}
public static String removeCharAt(int pos, String text) {
return text.substring(0, pos) + text.substring(pos + 1, text.length());
}
}
该计划的输出是:
size is : 720
[awedte, awedet, awetde, aweted, aweedt, aweetd, awdete, awdeet, awdtee, awdtee, awdeet, awdete, awtede, awteed, awtdee, awtdee, awteed, awtede, aweedt, aweetd, awedet, awedte, aweted, awetde, aewdte, aewdet, aewtde, aewted, aewedt, aewetd, aedwte, aedwet, aedtwe, aedtew, aedewt, aedetw, aetwde, aetwed, aetdwe, aetdew, aetewd, aetedw, aeewdt, aeewtd, aeedwt, aeedtw, aeetwd, aeetdw, adwete, adweet, adwtee, adwtee, adweet, adwete, adewte, adewet, adetwe, adetew, adeewt, adeetw, adtwee, adtwee, adtewe, adteew, adtewe, adteew, adewet, adewte, adeewt, adeetw, adetwe, adetew, atwede, atweed, atwdee, atwdee, atweed, atwede, atewde, atewed, atedwe, atedew, ateewd, ateedw, atdwee, atdwee, atdewe, atdeew, atdewe, atdeew, atewed, atewde, ateewd, ateedw, atedwe, atedew, aewedt, aewetd, aewdet, aewdte, aewted, aewtde, aeewdt, aeewtd, aeedwt, aeedtw, aeetwd, aeetdw, aedwet, aedwte, aedewt, aedetw, aedtwe, aedtew, aetwed, aetwde, aetewd, aetedw, aetdwe, aetdew, waedte, waedet, waetde, waeted, waeedt, waeetd, wadete, wadeet, wadtee, wadtee, wadeet, wadete, watede, wateed, watdee, watdee, wateed, watede, waeedt, waeetd, waedet, waedte, waeted, waetde, weadte, weadet, weatde, weated, weaedt, weaetd, wedate, wedaet, wedtae, wedtea, wedeat, wedeta, wetade, wetaed, wetdae, wetdea, wetead, weteda, weeadt, weeatd, weedat, weedta, weetad, weetda, wdaete, wdaeet, wdatee, wdatee, wdaeet, wdaete, wdeate, wdeaet, wdetae, wdetea, wdeeat, wdeeta, wdtaee, wdtaee, wdteae, wdteea, wdteae, wdteea, wdeaet, wdeate, wdeeat, wdeeta, wdetae, wdetea, wtaede, wtaeed, wtadee, wtadee, wtaeed, wtaede, wteade, wteaed, wtedae, wtedea, wteead, wteeda, wtdaee, wtdaee, wtdeae, wtdeea, wtdeae, wtdeea, wteaed, wteade, wteead, wteeda, wtedae, wtedea, weaedt, weaetd, weadet, weadte, weated, weatde, weeadt, weeatd, weedat, weedta, weetad, weetda, wedaet, wedate, wedeat, wedeta, wedtae, wedtea, wetaed, wetade, wetead, weteda, wetdae, wetdea, eawdte, eawdet, eawtde, eawted, eawedt, eawetd, eadwte, eadwet, eadtwe, eadtew, eadewt, eadetw, eatwde, eatwed, eatdwe, eatdew, eatewd, eatedw, eaewdt, eaewtd, eaedwt, eaedtw, eaetwd, eaetdw, ewadte, ewadet, ewatde, ewated, ewaedt, ewaetd, ewdate, ewdaet, ewdtae, ewdtea, ewdeat, ewdeta, ewtade, ewtaed, ewtdae, ewtdea, ewtead, ewteda, eweadt, eweatd, ewedat, ewedta, ewetad, ewetda, edawte, edawet, edatwe, edatew, edaewt, edaetw, edwate, edwaet, edwtae, edwtea, edweat, edweta, edtawe, edtaew, edtwae, edtwea, edteaw, edtewa, edeawt, edeatw, edewat, edewta, edetaw, edetwa, etawde, etawed, etadwe, etadew, etaewd, etaedw, etwade, etwaed, etwdae, etwdea, etwead, etweda, etdawe, etdaew, etdwae, etdwea, etdeaw, etdewa, eteawd, eteadw, etewad, etewda, etedaw, etedwa, eeawdt, eeawtd, eeadwt, eeadtw, eeatwd, eeatdw, eewadt, eewatd, eewdat, eewdta, eewtad, eewtda, eedawt, eedatw, eedwat, eedwta, eedtaw, eedtwa, eetawd, eetadw, eetwad, eetwda, eetdaw, eetdwa, dawete, daweet, dawtee, dawtee, daweet, dawete, daewte, daewet, daetwe, daetew, daeewt, daeetw, datwee, datwee, datewe, dateew, datewe, dateew, daewet, daewte, daeewt, daeetw, daetwe, daetew, dwaete, dwaeet, dwatee, dwatee, dwaeet, dwaete, dweate, dweaet, dwetae, dwetea, dweeat, dweeta, dwtaee, dwtaee, dwteae, dwteea, dwteae, dwteea, dweaet, dweate, dweeat, dweeta, dwetae, dwetea, deawte, deawet, deatwe, deatew, deaewt, deaetw, dewate, dewaet, dewtae, dewtea, deweat, deweta, detawe, detaew, detwae, detwea, deteaw, detewa, deeawt, deeatw, deewat, deewta, deetaw, deetwa, dtawee, dtawee, dtaewe, dtaeew, dtaewe, dtaeew, dtwaee, dtwaee, dtweae, dtweea, dtweae, dtweea, dteawe, dteaew, dtewae, dtewea, dteeaw, dteewa, dteawe, dteaew, dtewae, dtewea, dteeaw, dteewa, deawet, deawte, deaewt, deaetw, deatwe, deatew, dewaet, dewate, deweat, deweta, dewtae, dewtea, deeawt, deeatw, deewat, deewta, deetaw, deetwa, detawe, detaew, detwae, detwea, deteaw, detewa, tawede, taweed, tawdee, tawdee, taweed, tawede, taewde, taewed, taedwe, taedew, taeewd, taeedw, tadwee, tadwee, tadewe, tadeew, tadewe, tadeew, taewed, taewde, taeewd, taeedw, taedwe, taedew, twaede, twaeed, twadee, twadee, twaeed, twaede, tweade, tweaed, twedae, twedea, tweead, tweeda, twdaee, twdaee, twdeae, twdeea, twdeae, twdeea, tweaed, tweade, tweead, tweeda, twedae, twedea, teawde, teawed, teadwe, teadew, teaewd, teaedw, tewade, tewaed, tewdae, tewdea, tewead, teweda, tedawe, tedaew, tedwae, tedwea, tedeaw, tedewa, teeawd, teeadw, teewad, teewda, teedaw, teedwa, tdawee, tdawee, tdaewe, tdaeew, tdaewe, tdaeew, tdwaee, tdwaee, tdweae, tdweea, tdweae, tdweea, tdeawe, tdeaew, tdewae, tdewea, tdeeaw, tdeewa, tdeawe, tdeaew, tdewae, tdewea, tdeeaw, tdeewa, teawed, teawde, teaewd, teaedw, teadwe, teadew, tewaed, tewade, tewead, teweda, tewdae, tewdea, teeawd, teeadw, teewad, teewda, teedaw, teedwa, tedawe, tedaew, tedwae, tedwea, tedeaw, tedewa, eawedt, eawetd, eawdet, eawdte, eawted, eawtde, eaewdt, eaewtd, eaedwt, eaedtw, eaetwd, eaetdw, eadwet, eadwte, eadewt, eadetw, eadtwe, eadtew, eatwed, eatwde, eatewd, eatedw, eatdwe, eatdew, ewaedt, ewaetd, ewadet, ewadte, ewated, ewatde, eweadt, eweatd, ewedat, ewedta, ewetad, ewetda, ewdaet, ewdate, ewdeat, ewdeta, ewdtae, ewdtea, ewtaed, ewtade, ewtead, ewteda, ewtdae, ewtdea, eeawdt, eeawtd, eeadwt, eeadtw, eeatwd, eeatdw, eewadt, eewatd, eewdat, eewdta, eewtad, eewtda, eedawt, eedatw, eedwat, eedwta, eedtaw, eedtwa, eetawd, eetadw, eetwad, eetwda, eetdaw, eetdwa, edawet, edawte, edaewt, edaetw, edatwe, edatew, edwaet, edwate, edweat, edweta, edwtae, edwtea, edeawt, edeatw, edewat, edewta, edetaw, edetwa, edtawe, edtaew, edtwae, edtwea, edteaw, edtewa, etawed, etawde, etaewd, etaedw, etadwe, etadew, etwaed, etwade, etwead, etweda, etwdae, etwdea, eteawd, eteadw, etewad, etewda, etedaw, etedwa, etdawe, etdaew, etdwae, etdwea, etdeaw, etdewa]