开发者

How do I show all combinations?

I searched, can't find anything: How do I do this?

I enter $x = 'tlagre';

I want it to return every single letter combination, down to four.

i.e. - "tl开发者_StackOverflowagr" "gratl" "lat" "rat"

I've found some that can do this but none will do every number of letters (they all just do the exact same # of letters as given in $x


The search term you should probably be looking at is something like php string permutations instead of combinations. A permutation is an arrangement of items where the order of each item matters (so late and teal are different strings), while a combination is an arrangement where the order doesn't matter (late and teal are considered identical because they use the same letters).

Here's a few links from Google:

  • String permutation in PHP « Christer's blog o' fun
  • String Permutation Class - PHP Classes

Given an algorithm P that generates all the permutations of a string, an algorithm C that generates all the combinations of length N of some string, and a string S of length M, here's another algorithm that will generate all the permutations of S of length 1...M:

s = "..."
results = []
for (i = 1; i < s.length; ++i) {
  // All combinations of S of length i.
  combinations = C(s, i)

  // All permutations of S of length i.
  results.append(P(combinations))
}

// All permutations of S of length 1..[s.length].
return results
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜