Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7438551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:34:20+00:00 2026-05-29T10:34:20+00:00

I got a task that makes me a little crazy, there is the section

  • 0

I got a task that makes me a little crazy, there is the section dealing with word permutations, after I browsing the internet I found a function to do permutations, as shown below:

function permute($str) {
    if (strlen($str) < 2) {
        return array($str);
    }
    $permutations = array();
    $tail = substr($str, 1);
    foreach (permute($tail) as $permutation) {
        $length = strlen($permutation);
        for ($i = 0; $i <= $length; $i++) {
            $permutations[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
        }
    }

    return $permutations;
}

this to show the result:

print_r(array_unique(permute("abcdefghi"))); // found 362880
print_r(array_unique(permute("abcdefghij"))); // error

The problem is, this function is only able to perform all the permutations of 9 characters (approximately 362880 combinations, with a long time and make the browser not responding for tinytime). When trying to perform a permutation of up to 10 characters, an error message will appear:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes)

Do you have a solution or another way to do permutations with a length of 10 characters or more?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:34:21+00:00Added an answer on May 29, 2026 at 10:34 am

    The number of permutations of a string of length N is N!

    So if you’re just looking for the number of permutations, this will do:

    function factorial($n) {
        if( $n == 0) return 1;
        if( $n < 3) return $n;
        return $n*factorial($n-1);
    }
    function permute($str) {
        return factorial(strlen($str));
    }
    

    If, however, you are trying to get a random one of those permutations, try this:

    function permute($str) {
        $l = strlen($str);
        $a = str_split($str);
        $ret = "";
        while($l > 0) {
            $i = rand(0,$l-1);
            $ret .= $a[$i];
            array_splice($a,$i,1);
            $l--;
        }
        return $ret;
    }
    

    If you are trying to brute-force all N! permutations, try:

    ini_set("memory_limit",-1);
    set_time_limit(0);
    // your brute-force code here
    

    If none of these answer your question, please clarify 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a common task that I do with some Activities - downloading data
I've got a ant build.xml that uses the <copy> task to copy a variety
I got a task to maintain a c# project, I found some code like
Is there any handy tool that can make updating tables easier? Usually I got
Got a task to make a program that registers animals and the object is
I got the libraries that I needed to be recognized by the javac task,
So I've got a task that can be preformed by my GUI that will
So, I've got task from my boss to install opendocman on our newly installed
I've got the task of displaying a web form to represent the properties in
I've got the unpleasurable task of working on a Classic ASP site (VBSCRIPT) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.