Improving the time complexity of all permutations of a given string

Aks

The problem is generally posed as given a string, print all permutations of it. For eg, the permutations of string ABC are ABC, ACB, BAC, BCA, CAB, CBA.

The standard solution is a recursive one, given below.

void permute(char *a, int i, int n) 
{
   int j; 
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); //backtrack
       }
   }
}

This, runs into O(n*n!). Is this the best we can do or is there someway to make this faster?

son of the northern darkness

You can use std::next_permutation. Please, notice it works correctly only on sorted array.
Good points about this solution: 1) It is standard 2) It is non-recursive

Here is an example (http://www.cplusplus.com/reference/algorithm/next_permutation/):

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort

int main () {
  int myints[] = {1, 2, 3};

  std::sort (myints, myints + 3);

  std::cout << "The 3! possible permutations with 3 elements:\n";
  do {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while (std::next_permutation (myints, myints + 3));

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Time complexity of this code to list all permutations?

From Dev

Calculating and improving time & memory complexity (Java)

From Dev

all permutations of a string jquery

From Dev

What is the time complexity of the given algorthm?

From Dev

time complexity of this given data structure

From Dev

Time Complexity of the given nested loops

From Dev

What is the time complexity of the given code?

From Dev

Find the time complexity of a given function

From Dev

time complexity of this given data structure

From Dev

Time Complexity of the given nested loops

From Dev

Create algorithm with given time complexity

From Dev

What is the time complexity of given code?

From Dev

Improving Time Complexity of "increment the number represented as an array of digits"

From Dev

Find all possible permutations from a given set for specific string indices (PHP)

From Dev

Time complexity of string slice

From Dev

Create all possible permutations of given variable set

From Dev

Generate all possible permutations given input values

From Dev

Return permutations of all sizes of a string

From Dev

Print all Permutations of String by word

From Dev

Given node A find all nodes in A's subgraph with linear time complexity in Neo4j

From Dev

Given node A find all nodes in A's subgraph with linear time complexity in Neo4j

From Dev

Time complexity of program determining if two strings are permutations of each other

From Java

Time complexity of the given function with constant loop and recursion

From Dev

how to calculate time complexity for the given code?

From Dev

Determining the Time and Space complexity of a given code

From Dev

Confusion with time complexity in "Find a pair with the given difference"

From Dev

How to generate all permutations of a time series?

From Dev

How to generate all permutations of a time series?

From Dev

Find all matches of permutations within allotted time

Related Related

  1. 1

    Time complexity of this code to list all permutations?

  2. 2

    Calculating and improving time & memory complexity (Java)

  3. 3

    all permutations of a string jquery

  4. 4

    What is the time complexity of the given algorthm?

  5. 5

    time complexity of this given data structure

  6. 6

    Time Complexity of the given nested loops

  7. 7

    What is the time complexity of the given code?

  8. 8

    Find the time complexity of a given function

  9. 9

    time complexity of this given data structure

  10. 10

    Time Complexity of the given nested loops

  11. 11

    Create algorithm with given time complexity

  12. 12

    What is the time complexity of given code?

  13. 13

    Improving Time Complexity of "increment the number represented as an array of digits"

  14. 14

    Find all possible permutations from a given set for specific string indices (PHP)

  15. 15

    Time complexity of string slice

  16. 16

    Create all possible permutations of given variable set

  17. 17

    Generate all possible permutations given input values

  18. 18

    Return permutations of all sizes of a string

  19. 19

    Print all Permutations of String by word

  20. 20

    Given node A find all nodes in A's subgraph with linear time complexity in Neo4j

  21. 21

    Given node A find all nodes in A's subgraph with linear time complexity in Neo4j

  22. 22

    Time complexity of program determining if two strings are permutations of each other

  23. 23

    Time complexity of the given function with constant loop and recursion

  24. 24

    how to calculate time complexity for the given code?

  25. 25

    Determining the Time and Space complexity of a given code

  26. 26

    Confusion with time complexity in "Find a pair with the given difference"

  27. 27

    How to generate all permutations of a time series?

  28. 28

    How to generate all permutations of a time series?

  29. 29

    Find all matches of permutations within allotted time

HotTag

Archive