How to print all permutations of a given string in C, C ++, JavaScript and Python

0

A permutation is an arrangement of objects in a specific order. You can swap a string of length n into n! manners.

In this article, you will learn how to find all the permutations of a given string using C ++, Python, JavaScript, and C.

How do permutations work?

Let’s say you got some string str with “MUO” as string values. You have been asked to view the channel permutations. Here’s how you would go about it:

Example 1: Let str = “MUO”

The permutations of “MUO” are:

  • “MUO”

  • “SOFT”

  • “UMO”

  • “UOM”

  • “OUM”

  • “OMOU”

Note the order of the values. Here is another example:

Example 2: Let str = “AB”

All permutations of “AB” are:

You can also print duplicate permutations if there are repeated characters in the given string. (ABBA, for example)

Now that you understand how permutations work, let’s see how you can find them using your favorite programming language.

To note: We have designed the following code samples to generate permutations for three strings: MUO, AB, and XYZ. If you want to use any of these codes, copy it and modify these strings to suit your project.

Related: How To Convert Characters From A String In Opposite Case With Programming

C ++ program to print all permutations of a string

Below is the C ++ program to print all permutations of a string:

// C++ program to print all
// permutations of a string
#include
using namespace std;

// Function to print permutations of string
void findPermutations(string str, int leftIndex, int rightIndex)
{
if (leftIndex == rightIndex)
{
cout << str << endl;
}
else
{
for (int i = leftIndex; i <= rightIndex; i++)
{
swap(str[leftIndex], str[i]);
findPermutations(str, leftIndex+1, rightIndex);
//backtrack
swap(str[leftIndex], str[i]);
}
}
}
// Driver Code
int main()
{
string str1 = "MUO";
int size1 = str1.size();
cout << "str1: " << str1 << endl;
cout << "Permutations of " << str1 << ":" << endl;
findPermutations(str1, 0, size1-1);
string str2 = "AB";
int size2 = str2.size();
cout << "str2: " << str2 << endl;
cout << "Permutations of " << str2 << ":" << endl;
findPermutations(str2, 0, size2-1);
string str3 = "XYZ";
int size3 = str3.size();
cout << "str3: " << str3 << endl;
cout << "Permutations of " << str3 << ":" << endl;
findPermutations(str3, 0, size3-1);
return 0;
}

Go out:

str1: MUO
Permutations of MUO:
MUO
MOU
UMO
UOM
OUM
OMU
str2: AB
Permutations of AB:
AB
BA
str3: XYZ
Permutations of XYZ:
XYZ
XZY
YXZ
YZX
ZYX
ZXY

Related: How To Check If A Channel Is Balanced With Programming

Python program to print all permutations of a string

Next, is the Python code to print all permutations of a string:

# Python program to print all
# permutations of a string
def convertToString(List):
return ''.join(List)
# Function to print permutations of string
def findPermutations(s, leftIndex, rightIndex):
if leftIndex == rightIndex:
print(convertToString(s))
else:
for i in range(leftIndex, rightIndex+1):
s[leftIndex], s[i] = s[i], s[leftIndex]
findPermutations(s, leftIndex+1, rightIndex)
# backtrack
s[leftIndex], s[i] = s[i], s[leftIndex]
# Driver Code
str1 = "MUO"
size1 = len(str1)
s1 = list(str1)
print("str1:", str1)
print("Permutations of", str1,":")
findPermutations(s1, 0, size1-1)
str2 = "AB"
size2 = len(str2)
s2 = list(str2)
print("str2:", str2)
print("Permutations of", str2,":")
findPermutations(s2, 0, size2-1)
str3 = "XYZ"
size3 = len(str3)
s3 = list(str3)
print("str3:", str3)
print("Permutations of", str3,":")
findPermutations(s3, 0, size3-1)

Go out:

str1: MUO
Permutations of MUO:
MUO
MOU
UMO
UOM
OUM
OMU
str2: AB
Permutations of AB:
AB
BA
str3: XYZ
Permutations of XYZ:
XYZ
XZY
YXZ
YZX
ZYX
ZXY

Related: How To Check If Two Strings Are Anagrams Of Each Other

JavaScript program to print all permutations of a string

Here’s how to print permutations in JavaScript:

// JavaScript program to print all
// permutations of a string
// Function to swap characters of the string
function swap(str, leftIndex, i) {
let temp;
let tempArray = str.split("");
temp = tempArray[leftIndex] ;
tempArray[leftIndex] = tempArray[i];
tempArray[i] = temp;
return (tempArray).join("");
}
// Function to print permutations of string
function findPermutations(str, leftIndex, rightIndex) {
if (leftIndex == rightIndex) {
document.write(str + "
");
} else {
for (let i = leftIndex; i <= rightIndex; i++) {
str = swap(str, leftIndex, i);
findPermutations(str, leftIndex+1, rightIndex);
//backtrack
str = swap(str, leftIndex, i);;
}
}
}
// Driver Code
var str1 = "MUO";
var size1 = str1.length;
document.write("str1: " + str1 + "
");
document.write("Permutations of " + str1 + ":" + "
");
findPermutations(str1, 0, size1-1);
var str2 = "AB";
var size2 = str2.length;
document.write("str2: " + str2 + "
");
document.write("Permutations of " + str2 + ":" + "
");
findPermutations(str2, 0, size2-1);
var str3 = "XYZ";
var size3 = str3.length;
document.write("str3: " + str3 + "
");
document.write("Permutations of " + str3 + ":" + "
");
findPermutations(str3, 0, size3-1);

Go out:

str1: MUO
Permutations of MUO:
MUO
MOU
UMO
UOM
OUM
OMU
str2: AB
Permutations of AB:
AB
BA
str3: XYZ
Permutations of XYZ:
XYZ
XZY
YXZ
YZX
ZYX
ZXY

Related: How To Complete The FizzBuzz Challenge In 5 Programming Languages

C program to print all permutations of a string

Below is a C program that prints all the permutations of a string:

// C program to print all
// permutations of a string
#include
#include
// Function to swap characters of the string
void swap(char str[], int leftIndex, int i)
{
char temp = str[leftIndex];
str[leftIndex] = str[i];
str[i] = temp;
}
// Function to print permutations of string
void findPermutations(char str[], int leftIndex, int rightIndex)
{
if (leftIndex == rightIndex)
{
printf("%s ⁠n", str);
}
else
{
for (int i = leftIndex; i <= rightIndex; i++)
{
swap(str, leftIndex, i);
findPermutations(str, leftIndex+1, rightIndex);
//backtrack
swap(str, leftIndex, i);
}
}
}
// Driver Code
int main()
{
char str1[] = "MUO";
int size1 = strlen(str1);
printf("str1: %s ⁠n", str1);
printf("Permutations of %s: ⁠n", str1);
findPermutations(str1, 0, size1-1);
char str2[] = "AB";
int size2 = strlen(str2);
printf("str2: %s ⁠n", str2);
printf("Permutations of %s: ⁠n", str2);
findPermutations(str2, 0, size2-1);
char str3[] = "XYZ";
int size3 = strlen(str3);
printf("str3: %s ⁠n", str3);
printf("Permutations of %s: ⁠n", str3);
findPermutations(str3, 0, size3-1);
return 0;
}

Go out:

str1: MUO
Permutations of MUO:
MUO
MOU
UMO
UOM
OUM
OMU
str2: AB
Permutations of AB:
AB
BA
str3: XYZ
Permutations of XYZ:
XYZ
XZY
YXZ
YZX
ZYX
ZXY

Printing permutations is easy

In this article, you learned how to print all permutations of a string in multiple programming languages. While these sample programs aren’t the only way to handle permutations, they are a good start for those new to using them in their code.


free-online-programming-courses
The 11 Best Sites for Free Online Computer Programming Courses

By using these free online computer programming courses, you can become a great programmer without a computer degree.

Read more


About the Author


Source link

Leave A Reply

Your email address will not be published.