Hi all,

I have a PHP script that takes a string and lists all its permutations, but ONLY based on its length. So for instance, let's just say we had the word "racecar". Yes, it would list all permutations of the 7-character word "racecar" such as "acrerac", etc. but NOT words like "race" or "car" that have a smaller length. Here is my code:

function check($word) 
    {
        if (strlen($word) < 2) 
        {
            return array($word);
        }

        $permutations = array();

        $tail = substr($word, 1);

        foreach (check($tail) as $permutation) 
        {
            $length = strlen($permutation);

            for ($i = 0; $i <= $length; $i++) 
            {
                $permutations[] = substr($permutation, 0, $i) . $word[0] . substr($permutation, $i);
            }
        }

        return $permutations;
    }

Thanks in advance!

Member Avatar for LastMitch

@HelloJarvis

Yes, it would list all permutations of the 7-character word "racecar" such as "acrerac", etc. but NOT words like "race" or "car" that have a smaller length. Here is my code:

Here is a link to a example similiar to your and maybe you can figure it out:

http://cogo.wordpress.com/2008/01/08/string-permutation-in-php/

You also need an array in order it to work so here is an example with an array:

http://www.dannyherran.com/2011/06/finding-unique-array-combinations-with-php-permutations/

Here is an permutation formula to figure out the 7 character word:

http://www.mathwords.com/p/permutation_formula.htm

This is not what I mean :) I already found that algorithm on the web, but I want to mimic that just with varying string lengths. So instead of ONLY returning 7-character permutations for a 7-character word, it would return ALL permutations of ALL lengths less than or equal to 7.

Member Avatar for LastMitch

This is not what I mean :) I already found that algorithm on the web, but I want to mimic that just with varying string lengths. So instead of ONLY returning 7-character permutations for a 7-character word, it would return ALL permutations of ALL lengths less than or equal to 7.

Did you actually read this link:

http://cogo.wordpress.com/2008/01/08/string-permutation-in-php/

The example looks exactly like yours! You really need to take time and read when people provide a link. Noone will write the code for you!

6 letter word but wanted to output 4 letter permutations of the word

$final = substr($permutation, 0, 4)

I already saw that. I found that link before he posted it, and I saw that comment. I want to do this DYNAMICALLY, so EVERY possible length.

Member Avatar for diafol

You really need to do this? That's going to be one sloooow recursive function.

I think you're looking for 13699 perms.

I guess, I had considered the memory and time issue, but I'll try it and see how it goes. Could you post the code please?

Member Avatar for diafol

No. This isn't a 'write me free code' site. LM's links would be the ones I'd have suggested too. How about you have a little play with them and adapt them to your needs?

Well, the point is, I can't figure it out. I have the algorithm and the method of getting different lengths, I just don't know how to apply it.

Member Avatar for LastMitch

@HelloJarvis

Well, the point is, I can't figure it out. I have the algorithm and the method of getting different lengths, I just don't know how to apply it.

Try this

http://www.needcodefor.com/php/php-function-to-generate-all-permutations/

In the end you still need to used an array!

This is all we can help you! Unless you did some work on the code above because it look like you didn't bother play around with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.