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 7627909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:31:25+00:00 2026-05-31T05:31:25+00:00

I’ve been reviewing algorithms for practice, and I’m currently looking at a permutation algorithm

  • 0

I’ve been reviewing algorithms for practice, and I’m currently looking at a permutation algorithm that I quite like:

void permute(char* set, int begin, int end) {
    int range = end - begin;

    if (range == 1)
        cout << set << endl;
    else {
        for(int i = 0; i < range; ++i) {
            swap(&set[begin], &set[begin+i]);
            permute(set, begin+1, end);
            swap(&set[begin], &set[begin+i]);
        }
    }
}

I actually wanted to apply this to a situation where there will be many repeated characters though, so I need to be able to modify it to prevent the printing of duplicate permutations.

How would I go about detecting that I was generating a duplicate? I know I could store this in a hash or something similar, but that’s not an optimal solution – I’d prefer one that didn’t require extra storage. Can someone give me a suggestion?

PS: I don’t want to use the STL permutation mechanisms, and I don’t want a reference to another “unique permutation algorithm” somewhere. I’d like to understand the mechanism used to prevent duplication so I can build it into this in learn, if possible.

  • 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-31T05:31:26+00:00Added an answer on May 31, 2026 at 5:31 am

    There is no general way to prevent arbitrary functions from generating duplicates. You can always filter out the duplicates, of course, but you don’t want that, and for very good reasons. So you need a special way to generate only non-duplicates.

    One way would be to generate the permutations in increasing lexicographical order. Then you can just compare if a “new” permatutation is the same as the last one, and then skip it. It gets even better: the algorithm for generating permutations in increasing lexicographical order given at http://en.wikipedia.org/wiki/Permutations#Generation_in_lexicographic_order doesn’t even generate the duplicates at all!

    However, that is not an answer to your question, as it is a different algorithm (although based on swapping, too).

    So, let’s look at your algorithm a little closer. One key observation is:

    • Once a character is swapped to position begin, it will stay there for all nested calls of permute.

    We’ll combine this with the following general observation about permutations:

    • If you permute a string s, but only at positions where there’s the same character, s will remain the same. In fact, all duplicate permutations have a different order for the occurences of some character c, where c occurs at the same positions.

    OK, so all we have to do is to make sure that the occurences of each character are always in the same order as in the beginning. Code follows, but… I don’t really speak C++, so I’ll use Python and hope to get away with claiming it’s pseudo code.

    We start by your original algorithm, rewritten in ‘pseudo code’:

    def permute(s, begin, end):
        if end == begin + 1:
            print(s)
        else:
            for i in range(begin, end):
                s[begin], s[i] = s[i], s[begin]
                permute(s, begin + 1, end)
                s[begin], s[i] = s[i], s[begin]
    

    and a helper function that makes calling it easier:

    def permutations_w_duplicates(s):
        permute(list(s), 0, len(s)) # use a list, as in Python strings are not mutable
    

    Now we extend the permute function with some bookkeeping about how many times a certain character has been swapped to the begin position (i.e. has been fixed), and we also remember the original order of the occurences of each character (char_number). Each character that we try to swap to the begin position then has to be the next higher in the original order, i.e. the number of fixes for a character defines which original occurence of this character may be fixed next – I call this next_fixable.

    def permute2(s, next_fixable, char_number, begin, end):
        if end == begin + 1:
            print(s)
        else:
            for i in range(begin, end):
                if next_fixable[s[i]] == char_number[i]: 
                    next_fixable[s[i]] += 1
                    char_number[begin], char_number[i] = char_number[i], char_number[begin]
    
                    s[begin], s[i] = s[i], s[begin]
                    permute2(s, next_fixable, char_number, begin + 1, end)
                    s[begin], s[i] = s[i], s[begin]
    
                    char_number[begin], char_number[i] = char_number[i], char_number[begin]
                    next_fixable[s[i]] -= 1
    

    Again, we use a helper function:

    def permutations_wo_duplicates(s):
        alphabet = set(s)
        next_fixable = dict.fromkeys(alphabet, 0)
        count = dict.fromkeys(alphabet, 0)
        char_number = [0] * len(s)
        for i, c in enumerate(s):
            char_number[i] = count[c]
            count[c] += 1
    
        permute2(list(s), next_fixable, char_number, 0, len(s))
    

    That’s it!

    Almost. You can stop here and rewrite in C++ if you like, but if you are interested in some test data, read on.

    I used a slightly different code for testing, because I didn’t want to print all permutations. In Python, you would replace the print with a yield, with turns the function into a generator function, the result of which can be iterated over with a for loop, and the permutations will be computed only when needed. This is the real code and test I used:

    def permute2(s, next_fixable, char_number, begin, end):
        if end == begin + 1:
            yield "".join(s) # join the characters to form a string
        else:
            for i in range(begin, end):
                if next_fixable[s[i]] == char_number[i]:
                    next_fixable[s[i]] += 1
                    char_number[begin], char_number[i] = char_number[i], char_number[begin]
                    s[begin], s[i] = s[i], s[begin]
                    for p in permute2(s, next_fixable, char_number, begin + 1, end):
                        yield p
                    s[begin], s[i] = s[i], s[begin]
                    char_number[begin], char_number[i] = char_number[i], char_number[begin]
                    next_fixable[s[i]] -= 1
    
    def permutations_wo_duplicates(s):
        alphabet = set(s)
        next_fixable = dict.fromkeys(alphabet, 0)
        count = dict.fromkeys(alphabet, 0)
        char_number = [0] * len(s)
        for i, c in enumerate(s):
            char_number[i] = count[c]
            count[c] += 1
    
        for p in permute2(list(s), next_fixable, char_number, 0, len(s)):
            yield p
    
    
    s = "FOOQUUXFOO"
    A = list(permutations_w_duplicates(s))
    print("%s has %s permutations (counting duplicates)" % (s, len(A)))
    print("permutations of these that are unique: %s" % len(set(A)))
    B = list(permutations_wo_duplicates(s))
    print("%s has %s unique permutations (directly computed)" % (s, len(B)))
    
    print("The first 10 permutations       :", A[:10])
    print("The first 10 unique permutations:", B[:10])
    

    And the result:

    FOOQUUXFOO has 3628800 permutations (counting duplicates)
    permutations of these that are unique: 37800
    FOOQUUXFOO has 37800 unique permutations (directly computed)
    The first 10 permutations       : ['FOOQUUXFOO', 'FOOQUUXFOO', 'FOOQUUXOFO', 'FOOQUUXOOF', 'FOOQUUXOOF', 'FOOQUUXOFO', 'FOOQUUFXOO', 'FOOQUUFXOO', 'FOOQUUFOXO', 'FOOQUUFOOX']
    The first 10 unique permutations: ['FOOQUUXFOO', 'FOOQUUXOFO', 'FOOQUUXOOF', 'FOOQUUFXOO', 'FOOQUUFOXO', 'FOOQUUFOOX', 'FOOQUUOFXO', 'FOOQUUOFOX', 'FOOQUUOXFO', 'FOOQUUOXOF']
    

    Note that the permutations are computed in the same order than the original algorithm, just without the duplicates. Note that 37800 * 2! * 2! * 4! = 3628800, just like you would expect.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

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.