问题
I need to write a function which finds the possible fixed length combinations of a string. The need is that not all combis are required. For example, if the string is "abcde", and we need combis of length 3, then the function must return the following:
abc
abd
abe
acd
ace
ade
bcd
bde
bce
cde
and none else. I have been trying for it using recursion but things have not worked out as expected. I have also seen some similar questions but could not get much out of them. Algorithm or code(C, C++, Java), any help is welcome. Thanks.!
Note: The combinations need to be ordered. That is, the characters should follow the same order as in the input string.
回答1:
I expect it's possible to do better, but this is what I could come up with quickly.
I used a List to preserve order, and had to avoid duplicates awkwardly. Using a Set instead would allow us to skip the result.contains(s) check, but a better algorithm might avoid the duplicates in a cleaner way.
private static List<String> substrings(int i, String input) {
List<String> result = new ArrayList<String>();
if (i == 0)
return result;
String first = input.substring(0, i);
result.add(first);
if (input.length() == i) {
return result;
}
// Recursively find substrings of next smaller length not including the first character
List<String> tails = substrings(i-1, input.substring(1));
// Append first char to each result of the recursive call.
for (String sub: tails) {
String s = input.substring(0, 1) + sub;
if (!(result.contains(s)))
result.add(s);
}
// Add all substring of current length not including first character
result.addAll(substrings(i, input.substring(1)));
return result;
}
回答2:
In the case of having three inputs, you have three indexes, initially set to the first three indices of the input string. Print that out, then increase the last index by one, and print out all indexes. Continue until you have reached the end of the input string, then you increase the second index and reset the third to the next after the second. Continue until the second index is second to last character, then increase the first index, and place the second and third in consecutive order after the first. Continue...
Let me try to illustrate:
Input: [abcde]
^^^
123
Output: abc
Next iteration:
Input: [abcde]
^^ ^
12 3
Output: abd
Next iteration:
Input: [abcde]
^^ ^
12 3
Output: abe
Next iteration:
Input: [abcde]
^ ^^
1 23
Output: acd
Next iteration:
Input: [abcde]
^ ^ ^
1 2 3
Output: ace
Next iteration:
Input: [abcde]
^^^
123
Output: bcd
Next iteration:
Input: [abcde]
^^ ^
12 3
Output: bce
Next iteration:
Input: [abcde]
^ ^^
1 23
Output: bde
Next iteration:
Input: [abcde]
^^^
123
Output: cde
回答3:
Since your requirement is of a specific length of 3, split your
Stringto chunks of 3 and then apply the algo.Put aside one character from your
Stringof 3, and compute possible permutations for the remaining 2.Place that one character on all possible positions of the computed permutations.
Repeat above.
回答4:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
int main()
{
char buf[6] = "abcde";
int len = strlen(buf);
// std::vector<std::string> char_list;
for(int i = 0; i < len; ++i){
for(int j = i+1; j < len;++j){
//if (i==j)continue;
for(int k = j+1; k < len;++k){
//if(i == k || j == k)
// continue;
char temp[4]="";
temp[0] = buf[i];
temp[1] = buf[j];
temp[2] = buf[k];
temp[3] = '\0';
std::cout << temp << std::endl;
//show(temp,3);
//char_list.push_back(key);
}
}
}
return 0;
}
来源:https://stackoverflow.com/questions/12035147/ordered-fixed-length-combinations-of-a-string