我该如何解决这个可能的三个字母的单词


How can I solve this Possible three letter words

我想解决可能的三个字母单词的问题

Here is following words. (17 characters)
19920620forestJSR
How many possible ways to make 3 length word with given characters?
Ex: 192, 162, Rer, ….
Rule:
Number 0 is different alpha o. (0 != o)
case-sensitive is available. (R != r)
same character repeat not available. (rr1 : wrong)
Hint:
17 16 15 : wrong

我如何解决这个

我正在尝试使用此代码

function permute($str,$i,$n) {
   if ($i == $n)
       print "$str'n";
   else {
        for ($j = $i; $j < $n; $j++) {
          swap($str,$i,$j);
          permute($str, $i+1, $n);
          swap($str,$i,$j); // backtrack.
       }
   }
}

function swap(&$str,$i,$j) {
    $temp = $str[$i];
    $str[$i] = $str[$j];
    $str[$j] = $temp;
}   
$str='19920620forestJSR';
permute($str,0,strlen($str)); 

但在输出中,数字字符有一些错误

输出看起来像(当str=19920620forestJSR时)

n���Z��뢿�Yh��fzj+�ȳz��ߍ�シo+^��aj�-y�k��m��e�ƭ{�6�ټ�zȧo�h���j���Z�ǫ���������z�a���X�y�����

输出看起来像(当str=forestJSR时)

foresSJtR
foresSJRt
foresStJR
foresStRJ
foresSRtJ
foresSRJt
foresRJSt
foresRJtS

规则规定您需要3个字符的组合,不包含重复字母。您的代码正在生成17个字母的所有组合。因此,该脚本循环寻找数百万种可能性(355687428096000)。

一次取r的n个不同对象的排列次数为

nPr = n! / (n - r)! 

因此,对于"19920620forestJSR"(仅14个不同的字母),一次使用3个:

14P3 = 14! / (14 - 3)! = 14! / 11! = (14)(13)(12) = 2184
相关文章: