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

  • SEARCH
  • Home
  • 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 8817343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:49:25+00:00 2026-06-14T04:49:25+00:00

I know this topic is much discussed but I can’t seem to find any

  • 0

I know this topic is much discussed but I can’t seem to find any implementation that fits my needs.

I have the following set of characters:

a b c d e f g h

I want to get all possible permutations or combinations (non repeating), but on a limited (variable) set of characters, meaning if I input the characters and the number 2, the results should look like

ab ba ac ca ad da ae ea af fa ag ga ah ha
bc cb bd db be eb bf fb bg gb bh hb
cd dc ce ec cf fc cg gc ch hc
de ed df fd dg gd dh hd
ef fe eg ge eh he
fg gf fh hf
gh hg

I hope you understand where I’m going with this. I currently have an implementation that gives me the permutations of all characters, but I can’t wrap my head around how to implement a limited space for those permutations:

public function getPermutations($letters) {
    if (strlen($letters) < 2) {
        return array($letters);
    }

    $permutations = array();
    $tail = substr($letters, 1);

    foreach ($this->getPermutations($tail) as $permutation) {
        $length = strlen($permutation);

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

    return $permutations;
}
  • 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-06-14T04:49:27+00:00Added an answer on June 14, 2026 at 4:49 am

    If you only need one element at a time, you can save on memory by generating each element individually.

    If we wanted to generate a random string in your set of expected outputs, we could use this algorithm:

    Given a set of characters S, and a desired output length K:
      While the output has less than K characters:
        Pick a random number P between 1 and |S|.
        Append the P'th character to the output.
        Remove the P'th character from S.
    

    where |S| is the current number of elements in S.

    We can actually encode this sequence of choices into an integer. One way to do that is to change the algorithm as such:

    Given a set of characters S, and a desired output length K:
      Let I = 0.
      While the output has less than K characters:
        I = I * (|S| + 1).
        Pick a random number P between 1 and the number of elements in S.
        I = I + P.
        Append the P'th character to the output.
        Remove the P'th character from S.
    

    After running this algorithm, the value I will uniquely encode this particular sequence of choices. It basically encodes this as a mixed-radix number; one digit uses base N, the next uses N-1, and so on until the last digit which is base N-K+1 (N being the number of letters in the input).

    Naturally, we can also decode this again, and in PHP, that would be something like this:

    // Returns the total number of $count-length strings generatable from $letters.
    function getPermCount($letters, $count)
    {
      $result = 1;
      // k characters from a set of n has n!/(n-k)! possible combinations
      for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) {
        $result *= $i;
      }
      return $result;
    }
    
    // Decodes $index to a $count-length string from $letters, no repeat chars.
    function getPerm($letters, $count, $index)
    {
      $result = '';
      for($i = 0; $i < $count; $i++)
      {
        $pos = $index % strlen($letters);
        $result .= $letters[$pos];
        $index = ($index-$pos)/strlen($letters);
        $letters = substr($letters, 0, $pos) . substr($letters, $pos+1);
      }
      return $result;
    }
    

    (Note that for simplicity, this particular decoding algorithm does not correspond exactly to the encoding algorithm I previously described, but maintains the desirable property of a given $index mapping to a unique result.)

    To use this code, you would do something like this:

    $letters = 'abcd';
    echo '2 letters from 4:<br>';
    for($i = 0; $i < getPermCount($letters, 2); $i++)
      echo getPerm($letters, 2, $i).'<br>';
    
    echo '<br>3 letters from 4:<br>';
    for($i = 0; $i < getPermCount($letters, 3); $i++)
      echo getPerm($letters, 3, $i).'<br>';
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there is a lot on this topic but I can't get any
I know this is a much discussed topic.. But I am facing an issue
I know this topic has been discussed but I think it has some differences.
sorry, i know this lots on this topic, but i couldnt find one where
I know this is a recurrent/classical topic but I did not found anything that
I know there are already some posts about this topic, but I cannot find
I've been searching about this topic but i don't seem to find anything concrete
I know that there have been plenty of topics describing this topic but I
I know this topic was descibed few times so far, but here is mine
I know this topic is bit old, but i did surf the web 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.