简体   繁体   中英

How to get all the similar strings based on an array of letter variations

I keep trying to wrap my brain around this but I am effectively trying to generate an array/list of all variations of a given string based on an array of letter variations.

I have the string "fabien", and I have an array of variations for each letter involved. For instance A is replaceable with 4, i is replaceable with 1 and l. So given the information how can I generate a list of every variation of "fabien".

$variants = array();
$variants['a'] = array('4');
$variants['i'] = array('1', 'l');

$string = 'fabien';

$result = getVariants('fabien', $variants);

print_r($results);

// Sample output:
Array ([0] => fabien [1] => f4bien [2] => fab1en [3] => fablen [4] => f4b1en [5] => f4blen)

Your case can be easily implemented with recursion. That will be like:

function getVariants($string, $variants)
{
    //here's about stripping 1 symbol from string's right, so 
    //may be you'll prefer to work with string functions:
    $string  = is_array($string)?$string:str_split($string);
    $symbol  = array_pop($string);
    $variant = array_key_exists($symbol, $variants)?
               array_merge([$symbol], $variants[$symbol]):
               [$symbol];
    $result  = [];
    if(!count($string))
    {
        return $variant;
    }
    foreach(getVariants($string, $variants) as $piece)
    {
        foreach($variant as $char)
        {
            $result[] = $piece.$char;
        }
    }
    return $result;
}

-see fiddle demo. How is this working? The answer is: variation of string with length N is variations of it's right symbol 'multiplied' on variations of it's part without that symbol (ie with length N-1 ). By 'multiplication' I mean Decart product of two sets and then concatenation of two parts, that are in certain pair.

If you don't mind using nested function, try the below:

function getVariants($string, $variants)
{
    // Store extra params for the recusion
    function _getVariants($string, $variants, $batch, $i, &$results)
    {
        if ($i >= strlen($string))
        {
            $results[] = $batch;
        }
        else
        {
            $character = $string[$i];

            // By default, just concat the current character
            _getVariants($string, $variants, $batch . $character, $i + 1, $results);

            if ( ! empty($variants[$character]))
            {
                // If there is something to replace
                foreach ($variants[$character] as $character)
                {
                    // Concat the replaced character
                    _getVariants($string, $variants, $batch . $character, $i + 1, $results);
                }
            }
        }
    }

    $results = array();
    _getVariants($string, $variants, '', 0, $results);
    return $results;
}

Output:

Array ( [0] => fabien [1] => fab1en [2] => fablen [3] => f4bien [4] => f4b1en [5] => f4blen )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM