lolcoder
BAN USER<?php
$in = array('f', 'a', 'b');
$hash = array(
'f' => array('F', '4'),
'b' => array('B', '8')
);
getPerm($in, $hash);
function getPerm($in, $hash)
{
permHelper($in,sizeof($in)-1, $hash);
}
function permHelper($in,$i, $hash){
if($i == -1)
{
echo "\n".implode('',$in);
return;
}
permHelper($in,$i-1, $hash);
if(isset($hash[$in[$i]]))
{
foreach($hash[$in[$i]] as $rep)
{
$in[$i] = $rep;
permHelper($in,$i-1, $hash);
}
}
}
@barry, its 3*1*3 = 3^2 =9 not 2^3 = 8
and in other case it'll be O(m1*1*m2) complexity, no n there
given below working php code for this
<?php
$in = array('f', 'a', 'b');
$hash = array(
'f' => array('F', '4'),
'b' => array('B', '8')
);
getPerm($in, $hash);
function getPerm($in, $hash)
{
permHelper($in,sizeof($in)-1, $hash);
}
function permHelper($in,$i, $hash){
if($i == -1)
{
echo "\n".implode('',$in);
return;
}
permHelper($in,$i-1, $hash);
if(isset($hash[$in[$i]]))
{
foreach($hash[$in[$i]] as $rep)
{
$in[$i] = $rep;
permHelper($in,$i-1, $hash);
}
}
}
@melith, you might want to bound 2nd for loop over number of neighbors (always<=7)rather then on positions of certain element (max bound n2). let me know if i aam wrong or any other way to optimize that function any more
- lolcoder January 07, 2014