

How to Print All Permutations of a Given String in C, C++, JavaScript, and Pytho...
source link: https://www.makeuseof.com/how-to-print-all-permutations-of-a-given-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Print All Permutations of a Given String in C, C++, JavaScript, and Python
Published 9 hours ago
Need to print the permutations of a string? We'll show you how in several languages.
A permutation is an arrangement of objects in a specific order. You can permute a string of length n in n! ways.
In this article, you'll learn how to find all permutations of a given string using C++, Python, JavaScript, and C.
How Do Permutations Work?
Let's say you have string str with “MUO” as the string values. You've been asked to show the string's permutations. Here's how you'd go about it:
Example 1: Let str = "MUO”
The permutations of “MUO” are:
- “MUO”
- "MOU"
- “UMO”
- “UOM”
- “OUM”
- “OMU”
Note the order of the values. Here's another example:
Example 2: Let str = "AB”
All the permutations of “AB” are:
You can also print duplicate permutations if there are repeating characters in the given string. (ABBA, for example)
Now that you understand how permutations work, let's take a look at how you can find them using your preferred programming language.
Note: We've designed the following code examples to output permutations for three strings: MUO, AB, and XYZ. If you'd like to use any of this code, copy it, and change these strings to fit your project.
Related: How to Convert Characters of a String to the 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 <bits/stdc++.h>
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;
}
Output:
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 String Is Symmetrical 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)
Output:
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 Whether Two Strings Are Anagrams of Each Other
JavaScript Program to Print All Permutations of a String
Here's how you 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 + "<br>");
} 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 + "<br>");
document.write("Permutations of " + str1 + ":" + "<br>");
findPermutations(str1, 0, size1-1);
var str2 = "AB";
var size2 = str2.length;
document.write("str2: " + str2 + "<br>");
document.write("Permutations of " + str2 + ":" + "<br>");
findPermutations(str2, 0, size2-1);
var str3 = "XYZ";
var size3 = str3.length;
document.write("str3: " + str3 + "<br>");
document.write("Permutations of " + str3 + ":" + "<br>");
findPermutations(str3, 0, size3-1);
Output:
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 permutations of a string:
// C program to print all
// permutations of a string
#include <stdio.h>
#include <string.h>
// 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;
}
Output:
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've learned how to print all permutations of a string in several programming languages. While these sample programs aren't the only way to handle permutations, they're a great start for those who are new to using them in their code.
About The Author
.png?fit=crop&w=100&h=100)
Yuvraj Chandra (58 Articles Published)
Yuvraj is a Computer Science undergraduate student at the University of Delhi, India. He's passionate about Full Stack Web Development. When he's not writing, he's exploring the depth of different technologies.
Subscribe to our newsletter
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!
Recommend
-
13
A friend of mine (Hillel Kahana) shared with me a riddle that his 10-year old son brought from a math workshop. At first, the riddle sounded easy… The riddle You’ve got the first 6 digits 1,2,3,4,5 and 6. You have to...
-
8
Programmer and Software Interview Questions and Answers Write a method in Java that will find and print out all the possible combinations (or “permutations”) of the characters in a strin...
-
8
Tech Interview A site for technical interview questions, brain teasers, puzzles, quizzles (whatever the...
-
11
Question Given a string containing all distinct characters, find all permutations of it. [2] ...
-
16
Print a given matrix in spiral formSkip to content
-
18
Print All Distinct Elements of a given integer arraySkip to content
-
13
Print unique rows in a given boolean matrixSkip to content
-
12
Print the string after the specified character has occurred given no. of timesPrint the string after the specified character has occurred given no. of times10 Views09/06/2022
-
11
Java Program to Print all Permutations with Repetition of CharactersJava Program to Print all Permutations with Repetition of CharactersHello everyone. Welcome to Geeks for
-
8
JavaScript Program to Print All Numbers Divisible by 3 and 5 of a Given NumberSkip to content
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK