我有一个字符串"abcdefgklmno0123456789"
我需要枚举所有可能的组合,大小从6到7个字符。
aaaaaa aaaaab aaaaac ... 999999
答案 0 :(得分:3)
您可以使用Linq来执行此操作:
string s = "abcdefgklmno0123456789";
var pwdWith6Chars =
from a in s
from b in s
from c in s
from d in s
from e in s
from f in s
select new string(new[] { a, b, c, d, e, f });
var pwdWith7Chars =
from a in s
from b in s
from c in s
from d in s
from e in s
from f in s
from g in s
select new string(new[] { a, b, c, d, e, f, g });
var passwords = pwdWith6Chars.Concat(pwdWith7Chars).ToList();
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用递归来生成此列表。
伪代码(更像是C):
str = "abcdefgklmno0123456789"
res = empty string of 6 letters
results = list of string
function go(level) {
if (level == 6 or level == 7) {
add "res" to "results" list
if (level == 7) {
return
}
}
for(i=position; i<len(str); i++) {
res[level] = str[i]
go(level+1)
}
}
然后,你只需拨打go(0)。
您也可以使用yield运算符来创建迭代器,而不是使用结果列表。 迭代器具有节省内存的优势,因为您将产生每个组合而不是存储所有组合。