0
function permuteString($str) 
{ 
    $aStr = str_split($str);
    $iSize = count($aStr);
    $aResult = array();
    for ($i = 0; $i < $iSize; ++$i) 
    { 
        $sFirst = array_shift($aStr); 
        $aInner = $aStr; 
        $iInner = count($aInner); 
        for ($j = 0; $j < $iInner; ++$j) 
        { 
            $aResult[] = $sFirst . implode('', $aInner); 
            $sTmp = array_shift($aInner); 
            $aInner[] = $sTmp; 
        } 
        $aStr[] = $sFirst; 
    } 
    return $aResult; 
} 
$userinput = "7290"; 
print_r(permuteString($userinput));
    
[0] => 7290 
[1] => 7902 
[2] => 7029 
[3] => 2907 
[4] => 2079 
[5] => 2790 
[6] => 9072 
[7] => 9720 
[8] => 9207 
[9] => 0729 
[10] => 0297 
[11] => 0972

I get only 12 numbers Are there any probabilities to get more combinations in the given any 4 digits?

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • You should be able to use [this](https://stackoverflow.com/a/10223120/231316), just split your string into an array of characters. – Chris Haas Jan 25 '22 at 13:08

3 Answers3

0
$chars = str_split('7290');
sort($chars);

$result = [];

foreach ($chars as $char1) {
  $chars2 = array_diff($chars, [$char1]);
  foreach ($chars2 as $char2) {
    $chars3 = array_diff($chars2, [$char2]);
    foreach ($chars3 as $char3) {
      $chars4 = array_diff($chars3, [$char3]);
      foreach ($chars4 as $char4) {
        $result[] = $char1 . $char2 . $char3 . $char4;
      }
    }
  }
}

print_r($result);

This will print:

Array
(
    [0] => 0279
    [1] => 0297
    [2] => 0729
    [3] => 0792
    [4] => 0927
    [5] => 0972
    [6] => 2079
    [7] => 2097
    [8] => 2709
    [9] => 2790
    [10] => 2907
    [11] => 2970
    [12] => 7029
    [13] => 7092
    [14] => 7209
    [15] => 7290
    [16] => 7902
    [17] => 7920
    [18] => 9027
    [19] => 9072
    [20] => 9207
    [21] => 9270
    [22] => 9702
    [23] => 9720
)

The code will only work for 4 different digits (or characters).

lukas.j
  • 6,453
  • 2
  • 5
  • 24
  • Hi Lukas Awesome logic. Thanks for you awesome effort. but I have an issue. I want this to work in any 4 ONLY digits. This logic does not work if any single digit repeats. For example if input 7292. Thanks in advance. – Mohamed Habeeb Jan 26 '22 at 04:31
  • 'This logic does not work if any single digit repeats' – this contradicts the title of your post: _... of 4 digits numbers without repetition ..._. Anyway, I posted a second solution reflecting repeating digits. – lukas.j Jan 26 '22 at 13:10
0

This code allows for any combination of 4 digits, including repeating:

$chars = str_split('7220');
sort($chars);

$result = [];

foreach ($chars as $char1) {
  $chars2 = $chars;
  unset($chars2[array_search($char1, $chars, true)]);
  foreach ($chars2 as $char2) {
    $chars3 = $chars2;
    unset($chars3[array_search($char2, $chars2, true)]);
    foreach ($chars3 as $char3) {
      $chars4 = $chars3;
      unset($chars4[array_search($char3, $chars3, true)]);
      foreach ($chars4 as $char4) {
        $result[] = $char1 . $char2 . $char3 . $char4;
      }
    }
  }
}

$result = array_unique($result);

print_r($result);

This will print:

Array
(
    [0] => 0227
    [1] => 0272
    [2] => 0722
    [3] => 2027
    [4] => 2072
    [5] => 2207
    [6] => 2270
    [7] => 2702
    [8] => 2720
    [9] => 7022
    [10] => 7202
    [11] => 7220
)
lukas.j
  • 6,453
  • 2
  • 5
  • 24
0

A possible algorithm would be:

function sampling($chars, $size, $combinations = array()) {

if (empty($combinations)) {
    $combinations = $chars;
}

if ($size == 1) {
    return $combinations;
}

$new_combinations = array();

foreach ($combinations as $key=>$combination) {

    foreach ($chars as $char) {

        if($combination != $char && !preg_match("/{$char}/i", $combination)){
           
            $new_combinations[] = $combination . $char;
        }   
    }
}

return sampling($chars, $size - 1, $new_combinations);
}

$chars = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$output = sampling($chars, 4);
echo "<pre>";
print_r($output);
echo "</pre>";