For example, there is a line
String str = "program";
It is necessary to get all possible combinations, such as:
and so on.
How do I do this in java? Or at least tell me a general algorithm (with an example)?
It is not a problem to break the deadline, at least one character at a time, at least two, for example
for (int i = 0; i <str.length () - 1; i = i +2) {
String strTemp = str.charAt (i) + "" + str.charAt (i + 1);
System.out.println (strTemp);
}
But how to get (bypass) all possible combinations of one and two is not clear how to program.
you can do this using recursive programming:
public Set<String> combination(String str){
final Set<String> results = new HashSet<>();
if(str.length() == 0)
return results;
if(str.length() == 1){
results.add(str);
return results;
}
if(str.length() == 2){
results.add(str.charAt(0) + "-" + str.charAt(1));
results.add(str);
return results;
}
String head = str.substring(0, 1);
final Set<String> remaining = combination(str.substring(1));
remaining.forEach(s -> {
int i = s.indexOf("-");
if(i == 1){
results.add(head + s);
results.add(head + "-" + s);
} else if(s.length() == 2){
results.add(head + "-" + s);
} else if(i == 2){
results.add(head + "-" + s);
}
});
return results;
}
My suggestion:
Encode a single letter as a 0
and a pair as a 1
. Now all decompositions map to binary numbers. Enumerate all naturals, starting from 0
, up to 2^(L-2)
for a word of length L
, skipping the numbers such that 2O+Z>L
( O
ones and Z
zeroes).
Eg
p-r-o-g-r-a-m: 0000000
p-r-o-g-r-am: 000001
p-r-o-g-ra-m: 000010
p-r-o-gr-am: 00011
....
pr-o-g-r-a-m: 100000
I am not sure that this is efficient but for now I see no better way to enumerate than trying all naturals and skipping the incompatible ones.
Update:
Check this: What is the best way to loop through integers with at most k bits ON?
I like the recursive approach from hatef Alipoor. But i would put it in smaller methods to make it more readable.
Here is my code:
public class WordCombiner {
public static void main(String[] args) {
new WordCombiner("program").run();
}
private final String word;
public WordCombiner(String word) {
this.word = word;
}
public void run() {
resolveCobination("", 0);
}
private void resolveCobination(String builder, int currentIndex) {
appendSingleCharacter(builder, currentIndex);
if (currentIndex+1 < word.length()) {
appendDoubleCharacter(builder, currentIndex);
}
}
private void appendSingleCharacter(String builder, int currentIndex) {
builder = builder + word.charAt(currentIndex++);
resolveOrPrint(builder, currentIndex);
}
private void appendDoubleCharacter(String builder, int currentIndex) {
builder = builder + word.charAt(currentIndex++);
builder = builder + word.charAt(currentIndex++);
resolveOrPrint(builder, currentIndex);
}
private void resolveOrPrint(String builder, int currentIndex) {
if (currentIndex < word.length()) {
builder = builder + "-";
resolveCobination(builder, currentIndex);
} else {
System.out.println(builder);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.