0

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!

3
Contributors
9
Replies
10
Views
4 Years
Discussion Span
Last Post by LastMitch
0

@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

Edited by LastMitch: grammer

0

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.

0

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)

0

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.

1

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

I think you're looking for 13699 perms.

Edited by diafol

0

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?

0

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?

0

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.

0

@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.

Edited by LastMitch: grammer

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.