SOFTELメモ Developer's blog

会社概要 ブログ 調査依頼 採用情報 ...
技術者募集中

【php】すべての文字を1回ずつ使って作れる文字列の一覧を作る(順列)

問題

a,b,c,d,e,fの文字をすべて、1回ずつ使って作れる文字列をすべて書き出せ。

(異なるn個のものからr個とって1列に並べる順列の数は?)

解答例

順列ですね。

個数は ですね。

以下プログラムで一覧を書き出します。

/**
 * 順列作成関数
 *
 * @param $a 後方に付ける文字の配列
 * @param $s 前方の文字列
 * @return 文字の組み合わせ結果の配列
 */
function pat($a, $s = '')
{
  $r = array();
  if (count($a) && is_array($a)) {
    //第1引数に文字の配列を渡されたらループして処理
    foreach ($a as $k => $v) {
      //文字の配列から1文字もらってこちらにくっつけて
      $_s = $s . $v;
      //もらった1文字を除いた文字の配列を作って
      $_a = $a;
      unset($_a[$k]);
      //再帰呼び出し
      $_r = pat($_a, $_s);
      //返り値にする変数に結果を追加
      $r = array_merge($r, $_r);
    }
  } else {
    //第1引数が空っぽの配列だったら、再帰呼び出しはここでストップ
    //返り値はこれだけ
    $r[] = $s;
  }
  return $r;
}

$a = array('a', 'b', 'c', 'd', 'e', 'f');
var_export(pat($a));

実行結果

array (
  0 => 'abcdef',
  1 => 'abcdfe',
  2 => 'abcedf',
  3 => 'abcefd',
  4 => 'abcfde',
  5 => 'abcfed',
  6 => 'abdcef',
  7 => 'abdcfe',
  8 => 'abdecf',
  9 => 'abdefc',
  10 => 'abdfce',
  11 => 'abdfec',
  12 => 'abecdf',
  13 => 'abecfd',
  14 => 'abedcf',
  15 => 'abedfc',
  16 => 'abefcd',
  17 => 'abefdc',
  18 => 'abfcde',
  19 => 'abfced',
  20 => 'abfdce',
  21 => 'abfdec',
  22 => 'abfecd',
  23 => 'abfedc',
  24 => 'acbdef',
  25 => 'acbdfe',
  26 => 'acbedf',
  27 => 'acbefd',
  28 => 'acbfde',
  29 => 'acbfed',
  30 => 'acdbef',
  31 => 'acdbfe',
  32 => 'acdebf',
  33 => 'acdefb',
  34 => 'acdfbe',
  35 => 'acdfeb',
  36 => 'acebdf',
  37 => 'acebfd',
  38 => 'acedbf',
  39 => 'acedfb',
  40 => 'acefbd',
  41 => 'acefdb',
  42 => 'acfbde',
  43 => 'acfbed',
  44 => 'acfdbe',
  45 => 'acfdeb',
  46 => 'acfebd',
  47 => 'acfedb',
  48 => 'adbcef',
  49 => 'adbcfe',
  50 => 'adbecf',
  51 => 'adbefc',
  52 => 'adbfce',
  53 => 'adbfec',
  54 => 'adcbef',
  55 => 'adcbfe',
  56 => 'adcebf',
  57 => 'adcefb',
  58 => 'adcfbe',
  59 => 'adcfeb',
  60 => 'adebcf',
  61 => 'adebfc',
  62 => 'adecbf',
  63 => 'adecfb',
  64 => 'adefbc',
  65 => 'adefcb',
  66 => 'adfbce',
  67 => 'adfbec',
  68 => 'adfcbe',
  69 => 'adfceb',
  70 => 'adfebc',
  71 => 'adfecb',
  72 => 'aebcdf',
  73 => 'aebcfd',
  74 => 'aebdcf',
  75 => 'aebdfc',
  76 => 'aebfcd',
  77 => 'aebfdc',
  78 => 'aecbdf',
  79 => 'aecbfd',
  80 => 'aecdbf',
  81 => 'aecdfb',
  82 => 'aecfbd',
  83 => 'aecfdb',
  84 => 'aedbcf',
  85 => 'aedbfc',
  86 => 'aedcbf',
  87 => 'aedcfb',
  88 => 'aedfbc',
  89 => 'aedfcb',
  90 => 'aefbcd',
  91 => 'aefbdc',
  92 => 'aefcbd',
  93 => 'aefcdb',
  94 => 'aefdbc',
  95 => 'aefdcb',
  96 => 'afbcde',
  97 => 'afbced',
  98 => 'afbdce',
  99 => 'afbdec',
  100 => 'afbecd',
  101 => 'afbedc',
  102 => 'afcbde',
  103 => 'afcbed',
  104 => 'afcdbe',
  105 => 'afcdeb',
  106 => 'afcebd',
  107 => 'afcedb',
  108 => 'afdbce',
  109 => 'afdbec',
  110 => 'afdcbe',
  111 => 'afdceb',
  112 => 'afdebc',
  113 => 'afdecb',
  114 => 'afebcd',
  115 => 'afebdc',
  116 => 'afecbd',
  117 => 'afecdb',
  118 => 'afedbc',
  119 => 'afedcb',
  120 => 'bacdef',
  121 => 'bacdfe',
  122 => 'bacedf',
  123 => 'bacefd',
  124 => 'bacfde',
  125 => 'bacfed',
  126 => 'badcef',
  127 => 'badcfe',
  128 => 'badecf',
  129 => 'badefc',
  130 => 'badfce',
  131 => 'badfec',
  132 => 'baecdf',
  133 => 'baecfd',
  134 => 'baedcf',
  135 => 'baedfc',
  136 => 'baefcd',
  137 => 'baefdc',
  138 => 'bafcde',
  139 => 'bafced',
  140 => 'bafdce',
  141 => 'bafdec',
  142 => 'bafecd',
  143 => 'bafedc',
  144 => 'bcadef',
  145 => 'bcadfe',
  146 => 'bcaedf',
  147 => 'bcaefd',
  148 => 'bcafde',
  149 => 'bcafed',
  150 => 'bcdaef',
  151 => 'bcdafe',
  152 => 'bcdeaf',
  153 => 'bcdefa',
  154 => 'bcdfae',
  155 => 'bcdfea',
  156 => 'bceadf',
  157 => 'bceafd',
  158 => 'bcedaf',
  159 => 'bcedfa',
  160 => 'bcefad',
  161 => 'bcefda',
  162 => 'bcfade',
  163 => 'bcfaed',
  164 => 'bcfdae',
  165 => 'bcfdea',
  166 => 'bcfead',
  167 => 'bcfeda',
  168 => 'bdacef',
  169 => 'bdacfe',
  170 => 'bdaecf',
  171 => 'bdaefc',
  172 => 'bdafce',
  173 => 'bdafec',
  174 => 'bdcaef',
  175 => 'bdcafe',
  176 => 'bdceaf',
  177 => 'bdcefa',
  178 => 'bdcfae',
  179 => 'bdcfea',
  180 => 'bdeacf',
  181 => 'bdeafc',
  182 => 'bdecaf',
  183 => 'bdecfa',
  184 => 'bdefac',
  185 => 'bdefca',
  186 => 'bdface',
  187 => 'bdfaec',
  188 => 'bdfcae',
  189 => 'bdfcea',
  190 => 'bdfeac',
  191 => 'bdfeca',
  192 => 'beacdf',
  193 => 'beacfd',
  194 => 'beadcf',
  195 => 'beadfc',
  196 => 'beafcd',
  197 => 'beafdc',
  198 => 'becadf',
  199 => 'becafd',
  200 => 'becdaf',
  201 => 'becdfa',
  202 => 'becfad',
  203 => 'becfda',
  204 => 'bedacf',
  205 => 'bedafc',
  206 => 'bedcaf',
  207 => 'bedcfa',
  208 => 'bedfac',
  209 => 'bedfca',
  210 => 'befacd',
  211 => 'befadc',
  212 => 'befcad',
  213 => 'befcda',
  214 => 'befdac',
  215 => 'befdca',
  216 => 'bfacde',
  217 => 'bfaced',
  218 => 'bfadce',
  219 => 'bfadec',
  220 => 'bfaecd',
  221 => 'bfaedc',
  222 => 'bfcade',
  223 => 'bfcaed',
  224 => 'bfcdae',
  225 => 'bfcdea',
  226 => 'bfcead',
  227 => 'bfceda',
  228 => 'bfdace',
  229 => 'bfdaec',
  230 => 'bfdcae',
  231 => 'bfdcea',
  232 => 'bfdeac',
  233 => 'bfdeca',
  234 => 'bfeacd',
  235 => 'bfeadc',
  236 => 'bfecad',
  237 => 'bfecda',
  238 => 'bfedac',
  239 => 'bfedca',
  240 => 'cabdef',
  241 => 'cabdfe',
  242 => 'cabedf',
  243 => 'cabefd',
  244 => 'cabfde',
  245 => 'cabfed',
  246 => 'cadbef',
  247 => 'cadbfe',
  248 => 'cadebf',
  249 => 'cadefb',
  250 => 'cadfbe',
  251 => 'cadfeb',
  252 => 'caebdf',
  253 => 'caebfd',
  254 => 'caedbf',
  255 => 'caedfb',
  256 => 'caefbd',
  257 => 'caefdb',
  258 => 'cafbde',
  259 => 'cafbed',
  260 => 'cafdbe',
  261 => 'cafdeb',
  262 => 'cafebd',
  263 => 'cafedb',
  264 => 'cbadef',
  265 => 'cbadfe',
  266 => 'cbaedf',
  267 => 'cbaefd',
  268 => 'cbafde',
  269 => 'cbafed',
  270 => 'cbdaef',
  271 => 'cbdafe',
  272 => 'cbdeaf',
  273 => 'cbdefa',
  274 => 'cbdfae',
  275 => 'cbdfea',
  276 => 'cbeadf',
  277 => 'cbeafd',
  278 => 'cbedaf',
  279 => 'cbedfa',
  280 => 'cbefad',
  281 => 'cbefda',
  282 => 'cbfade',
  283 => 'cbfaed',
  284 => 'cbfdae',
  285 => 'cbfdea',
  286 => 'cbfead',
  287 => 'cbfeda',
  288 => 'cdabef',
  289 => 'cdabfe',
  290 => 'cdaebf',
  291 => 'cdaefb',
  292 => 'cdafbe',
  293 => 'cdafeb',
  294 => 'cdbaef',
  295 => 'cdbafe',
  296 => 'cdbeaf',
  297 => 'cdbefa',
  298 => 'cdbfae',
  299 => 'cdbfea',
  300 => 'cdeabf',
  301 => 'cdeafb',
  302 => 'cdebaf',
  303 => 'cdebfa',
  304 => 'cdefab',
  305 => 'cdefba',
  306 => 'cdfabe',
  307 => 'cdfaeb',
  308 => 'cdfbae',
  309 => 'cdfbea',
  310 => 'cdfeab',
  311 => 'cdfeba',
  312 => 'ceabdf',
  313 => 'ceabfd',
  314 => 'ceadbf',
  315 => 'ceadfb',
  316 => 'ceafbd',
  317 => 'ceafdb',
  318 => 'cebadf',
  319 => 'cebafd',
  320 => 'cebdaf',
  321 => 'cebdfa',
  322 => 'cebfad',
  323 => 'cebfda',
  324 => 'cedabf',
  325 => 'cedafb',
  326 => 'cedbaf',
  327 => 'cedbfa',
  328 => 'cedfab',
  329 => 'cedfba',
  330 => 'cefabd',
  331 => 'cefadb',
  332 => 'cefbad',
  333 => 'cefbda',
  334 => 'cefdab',
  335 => 'cefdba',
  336 => 'cfabde',
  337 => 'cfabed',
  338 => 'cfadbe',
  339 => 'cfadeb',
  340 => 'cfaebd',
  341 => 'cfaedb',
  342 => 'cfbade',
  343 => 'cfbaed',
  344 => 'cfbdae',
  345 => 'cfbdea',
  346 => 'cfbead',
  347 => 'cfbeda',
  348 => 'cfdabe',
  349 => 'cfdaeb',
  350 => 'cfdbae',
  351 => 'cfdbea',
  352 => 'cfdeab',
  353 => 'cfdeba',
  354 => 'cfeabd',
  355 => 'cfeadb',
  356 => 'cfebad',
  357 => 'cfebda',
  358 => 'cfedab',
  359 => 'cfedba',
  360 => 'dabcef',
  361 => 'dabcfe',
  362 => 'dabecf',
  363 => 'dabefc',
  364 => 'dabfce',
  365 => 'dabfec',
  366 => 'dacbef',
  367 => 'dacbfe',
  368 => 'dacebf',
  369 => 'dacefb',
  370 => 'dacfbe',
  371 => 'dacfeb',
  372 => 'daebcf',
  373 => 'daebfc',
  374 => 'daecbf',
  375 => 'daecfb',
  376 => 'daefbc',
  377 => 'daefcb',
  378 => 'dafbce',
  379 => 'dafbec',
  380 => 'dafcbe',
  381 => 'dafceb',
  382 => 'dafebc',
  383 => 'dafecb',
  384 => 'dbacef',
  385 => 'dbacfe',
  386 => 'dbaecf',
  387 => 'dbaefc',
  388 => 'dbafce',
  389 => 'dbafec',
  390 => 'dbcaef',
  391 => 'dbcafe',
  392 => 'dbceaf',
  393 => 'dbcefa',
  394 => 'dbcfae',
  395 => 'dbcfea',
  396 => 'dbeacf',
  397 => 'dbeafc',
  398 => 'dbecaf',
  399 => 'dbecfa',
  400 => 'dbefac',
  401 => 'dbefca',
  402 => 'dbface',
  403 => 'dbfaec',
  404 => 'dbfcae',
  405 => 'dbfcea',
  406 => 'dbfeac',
  407 => 'dbfeca',
  408 => 'dcabef',
  409 => 'dcabfe',
  410 => 'dcaebf',
  411 => 'dcaefb',
  412 => 'dcafbe',
  413 => 'dcafeb',
  414 => 'dcbaef',
  415 => 'dcbafe',
  416 => 'dcbeaf',
  417 => 'dcbefa',
  418 => 'dcbfae',
  419 => 'dcbfea',
  420 => 'dceabf',
  421 => 'dceafb',
  422 => 'dcebaf',
  423 => 'dcebfa',
  424 => 'dcefab',
  425 => 'dcefba',
  426 => 'dcfabe',
  427 => 'dcfaeb',
  428 => 'dcfbae',
  429 => 'dcfbea',
  430 => 'dcfeab',
  431 => 'dcfeba',
  432 => 'deabcf',
  433 => 'deabfc',
  434 => 'deacbf',
  435 => 'deacfb',
  436 => 'deafbc',
  437 => 'deafcb',
  438 => 'debacf',
  439 => 'debafc',
  440 => 'debcaf',
  441 => 'debcfa',
  442 => 'debfac',
  443 => 'debfca',
  444 => 'decabf',
  445 => 'decafb',
  446 => 'decbaf',
  447 => 'decbfa',
  448 => 'decfab',
  449 => 'decfba',
  450 => 'defabc',
  451 => 'defacb',
  452 => 'defbac',
  453 => 'defbca',
  454 => 'defcab',
  455 => 'defcba',
  456 => 'dfabce',
  457 => 'dfabec',
  458 => 'dfacbe',
  459 => 'dfaceb',
  460 => 'dfaebc',
  461 => 'dfaecb',
  462 => 'dfbace',
  463 => 'dfbaec',
  464 => 'dfbcae',
  465 => 'dfbcea',
  466 => 'dfbeac',
  467 => 'dfbeca',
  468 => 'dfcabe',
  469 => 'dfcaeb',
  470 => 'dfcbae',
  471 => 'dfcbea',
  472 => 'dfceab',
  473 => 'dfceba',
  474 => 'dfeabc',
  475 => 'dfeacb',
  476 => 'dfebac',
  477 => 'dfebca',
  478 => 'dfecab',
  479 => 'dfecba',
  480 => 'eabcdf',
  481 => 'eabcfd',
  482 => 'eabdcf',
  483 => 'eabdfc',
  484 => 'eabfcd',
  485 => 'eabfdc',
  486 => 'eacbdf',
  487 => 'eacbfd',
  488 => 'eacdbf',
  489 => 'eacdfb',
  490 => 'eacfbd',
  491 => 'eacfdb',
  492 => 'eadbcf',
  493 => 'eadbfc',
  494 => 'eadcbf',
  495 => 'eadcfb',
  496 => 'eadfbc',
  497 => 'eadfcb',
  498 => 'eafbcd',
  499 => 'eafbdc',
  500 => 'eafcbd',
  501 => 'eafcdb',
  502 => 'eafdbc',
  503 => 'eafdcb',
  504 => 'ebacdf',
  505 => 'ebacfd',
  506 => 'ebadcf',
  507 => 'ebadfc',
  508 => 'ebafcd',
  509 => 'ebafdc',
  510 => 'ebcadf',
  511 => 'ebcafd',
  512 => 'ebcdaf',
  513 => 'ebcdfa',
  514 => 'ebcfad',
  515 => 'ebcfda',
  516 => 'ebdacf',
  517 => 'ebdafc',
  518 => 'ebdcaf',
  519 => 'ebdcfa',
  520 => 'ebdfac',
  521 => 'ebdfca',
  522 => 'ebfacd',
  523 => 'ebfadc',
  524 => 'ebfcad',
  525 => 'ebfcda',
  526 => 'ebfdac',
  527 => 'ebfdca',
  528 => 'ecabdf',
  529 => 'ecabfd',
  530 => 'ecadbf',
  531 => 'ecadfb',
  532 => 'ecafbd',
  533 => 'ecafdb',
  534 => 'ecbadf',
  535 => 'ecbafd',
  536 => 'ecbdaf',
  537 => 'ecbdfa',
  538 => 'ecbfad',
  539 => 'ecbfda',
  540 => 'ecdabf',
  541 => 'ecdafb',
  542 => 'ecdbaf',
  543 => 'ecdbfa',
  544 => 'ecdfab',
  545 => 'ecdfba',
  546 => 'ecfabd',
  547 => 'ecfadb',
  548 => 'ecfbad',
  549 => 'ecfbda',
  550 => 'ecfdab',
  551 => 'ecfdba',
  552 => 'edabcf',
  553 => 'edabfc',
  554 => 'edacbf',
  555 => 'edacfb',
  556 => 'edafbc',
  557 => 'edafcb',
  558 => 'edbacf',
  559 => 'edbafc',
  560 => 'edbcaf',
  561 => 'edbcfa',
  562 => 'edbfac',
  563 => 'edbfca',
  564 => 'edcabf',
  565 => 'edcafb',
  566 => 'edcbaf',
  567 => 'edcbfa',
  568 => 'edcfab',
  569 => 'edcfba',
  570 => 'edfabc',
  571 => 'edfacb',
  572 => 'edfbac',
  573 => 'edfbca',
  574 => 'edfcab',
  575 => 'edfcba',
  576 => 'efabcd',
  577 => 'efabdc',
  578 => 'efacbd',
  579 => 'efacdb',
  580 => 'efadbc',
  581 => 'efadcb',
  582 => 'efbacd',
  583 => 'efbadc',
  584 => 'efbcad',
  585 => 'efbcda',
  586 => 'efbdac',
  587 => 'efbdca',
  588 => 'efcabd',
  589 => 'efcadb',
  590 => 'efcbad',
  591 => 'efcbda',
  592 => 'efcdab',
  593 => 'efcdba',
  594 => 'efdabc',
  595 => 'efdacb',
  596 => 'efdbac',
  597 => 'efdbca',
  598 => 'efdcab',
  599 => 'efdcba',
  600 => 'fabcde',
  601 => 'fabced',
  602 => 'fabdce',
  603 => 'fabdec',
  604 => 'fabecd',
  605 => 'fabedc',
  606 => 'facbde',
  607 => 'facbed',
  608 => 'facdbe',
  609 => 'facdeb',
  610 => 'facebd',
  611 => 'facedb',
  612 => 'fadbce',
  613 => 'fadbec',
  614 => 'fadcbe',
  615 => 'fadceb',
  616 => 'fadebc',
  617 => 'fadecb',
  618 => 'faebcd',
  619 => 'faebdc',
  620 => 'faecbd',
  621 => 'faecdb',
  622 => 'faedbc',
  623 => 'faedcb',
  624 => 'fbacde',
  625 => 'fbaced',
  626 => 'fbadce',
  627 => 'fbadec',
  628 => 'fbaecd',
  629 => 'fbaedc',
  630 => 'fbcade',
  631 => 'fbcaed',
  632 => 'fbcdae',
  633 => 'fbcdea',
  634 => 'fbcead',
  635 => 'fbceda',
  636 => 'fbdace',
  637 => 'fbdaec',
  638 => 'fbdcae',
  639 => 'fbdcea',
  640 => 'fbdeac',
  641 => 'fbdeca',
  642 => 'fbeacd',
  643 => 'fbeadc',
  644 => 'fbecad',
  645 => 'fbecda',
  646 => 'fbedac',
  647 => 'fbedca',
  648 => 'fcabde',
  649 => 'fcabed',
  650 => 'fcadbe',
  651 => 'fcadeb',
  652 => 'fcaebd',
  653 => 'fcaedb',
  654 => 'fcbade',
  655 => 'fcbaed',
  656 => 'fcbdae',
  657 => 'fcbdea',
  658 => 'fcbead',
  659 => 'fcbeda',
  660 => 'fcdabe',
  661 => 'fcdaeb',
  662 => 'fcdbae',
  663 => 'fcdbea',
  664 => 'fcdeab',
  665 => 'fcdeba',
  666 => 'fceabd',
  667 => 'fceadb',
  668 => 'fcebad',
  669 => 'fcebda',
  670 => 'fcedab',
  671 => 'fcedba',
  672 => 'fdabce',
  673 => 'fdabec',
  674 => 'fdacbe',
  675 => 'fdaceb',
  676 => 'fdaebc',
  677 => 'fdaecb',
  678 => 'fdbace',
  679 => 'fdbaec',
  680 => 'fdbcae',
  681 => 'fdbcea',
  682 => 'fdbeac',
  683 => 'fdbeca',
  684 => 'fdcabe',
  685 => 'fdcaeb',
  686 => 'fdcbae',
  687 => 'fdcbea',
  688 => 'fdceab',
  689 => 'fdceba',
  690 => 'fdeabc',
  691 => 'fdeacb',
  692 => 'fdebac',
  693 => 'fdebca',
  694 => 'fdecab',
  695 => 'fdecba',
  696 => 'feabcd',
  697 => 'feabdc',
  698 => 'feacbd',
  699 => 'feacdb',
  700 => 'feadbc',
  701 => 'feadcb',
  702 => 'febacd',
  703 => 'febadc',
  704 => 'febcad',
  705 => 'febcda',
  706 => 'febdac',
  707 => 'febdca',
  708 => 'fecabd',
  709 => 'fecadb',
  710 => 'fecbad',
  711 => 'fecbda',
  712 => 'fecdab',
  713 => 'fecdba',
  714 => 'fedabc',
  715 => 'fedacb',
  716 => 'fedbac',
  717 => 'fedbca',
  718 => 'fedcab',
  719 => 'fedcba',
)

注意

文字数が9文字、10文字を越えたあたりから、件数が何十万件、何百万件となり、劇的に遅くなっていくと思います。

メモリ上で単純な総当りをしていますが、文字数注意です。

関連するメモ

コメント(1)

匿名 2015年1月9日 19:09

5文字の組合せを作りたいのですが、桁数を指定する場合はどうすれば良いのでしょうか。
よろしくお願いいたします。