Q. Write a program to print all permutations of a given string.



permutations :- In mathematics, A permutation is an arrangement of objects in a definite order.
For Example :-
Input = ABC
Output = ABC, ACB, BAC, BCA, CBA, CAB
So as we can see that all permutations of a given string ABC is ABC, ACB, BAC, BCA, CBA, CAB.

All Permutations of Given String Algorithm

START
if left = right, then
display str
else
for i := left to right, do
swap str[left] and str[i]
stringPermutation(str, left+1, right)
swap str[left] and str[i]             //for backtrack
done
STOP


All Permutations of Given String

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • // C program to print all permutations with duplicates allowed 
    #include <stdio.h>
    #include <string.h>
    /* Function to swap values at two pointers */
    void swap(char *num1, char *num2) 
    { 
    char temp; 
    temp = *num1; 
    *num1 = *num2; 
    *num2 = temp; 
    } 
    void find_permute(char *a, int len1, int len2) 
    { 
    int i; 
    if (len1 == len2) 
    printf("%s\n", a); 
    else
    { 
    for (i = len1; i <= len2; i++) 
    { 
    swap((a+len1), (a+i)); 
    find_permute(a, len1+1, len2); 
    swap((a+len1), (a+i)); //backtrack 
    } 
    } 
    } 
    int main() 
    { 
    char str[] = "LFC"; 
    int len = strlen(str); 
    find_permute(str, 0, len-1); 
    return 0; 
    } 
    
    #include <bits/stdc++.h>
    using namespace std; 
    void find_permute(string a, int len1, int len2)  
    {  
    // Base case  
    if (len1 == len2)  
    cout<< a<< endl;  
    else
    {  
    // Permutations made  
    for (int i = len1; i <= len2; i++)  
    {  
    // Swapping done  
    swap(a[len1], a[i]);  
    // Recursion called  
    find_permute(a, len1+1, len2);  
    //backtrack  
    swap(a[len1], a[i]);  
    }  
    }  
    }  
    int main()  
    {  
    string str = "LFC";  
    int len = str.size();  
    find_permute(str, 0, len-1);  
    return 0;  
    }  
    
    public class Main 
    { 
    public static void main(String[] args) 
    { 
    String str = "LFC"; 
    int len = str.length(); 
    Main permutation = new Main(); 
    permutation.find_permute(str, 0, len-1); 
    } 
    private void find_permute(String str, int len1, int len2) 
    { 
    if (len1 == len2) 
    System.out.println(str); 
    else
    { 
    for (int i = len1; i <= len2; i++) 
    { 
    str = swap(str,len1,i); 
    find_permute(str, len1+1, len2); 
    str = swap(str,len1,i); 
    } 
    } 
    } 
    public String swap(String a, int i, int j) 
    { 
    char temp; 
    char[] charArray = a.toCharArray(); 
    temp = charArray[i] ; 
    charArray[i] = charArray[j]; 
    charArray[j] = temp; 
    return String.valueOf(charArray); 
    } 
    } 
    
    def permute_string(str):
    if len(str) == 0:
    return ['']
    prev_list = permute_string(str[1:len(str)])
    next_list = []
    for i in range(0,len(prev_list)):
    for j in range(0,len(str)):
    new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1]
    if new_str not in next_list:
    next_list.append(new_str)
    return next_list
    print(permute_string('LFC'));
    
    using System; 
    class LFC 
    { 
    private static void find_permute(String str, 
    int len1, int len2) 
    { 
    if (len1 == len2) 
    Console.WriteLine(str); 
    else
    { 
    for (int i = len1; i <= len2; i++) 
    { 
    str = swap(str, len1, i); 
    find_permute(str, len1 + 1, len2); 
    str = swap(str, len1, i); 
    } 
    } 
    } 
    public static String swap(String a,  
    int i, int j) 
    { 
    char temp; 
    char[] charArray = a.ToCharArray(); 
    temp = charArray[i] ; 
    charArray[i] = charArray[j]; 
    charArray[j] = temp; 
    string s = new string(charArray); 
    return s; 
    } 
    // Driver Code 
    public static void Main() 
    { 
    String str = "LFC"; 
    int len = str.Length; 
    find_permute(str, 0, len-1); 
    } 
    } 
    
    function find_permute($str, $len1, $len2) 
    { 
    if ($len1 == $len2) 
    echo $str. "\n"; 
    else
    { 
    for ($i = $len1; $i <= $len2; $i++) 
    { 
    $str = swap($str, $len1, $i); 
    find_permute($str, $len1 + 1, $len2); 
    $str = swap($str, $len1, $i); 
    } 
    } 
    } 
    function swap($a, $i, $j) 
    { 
    $temp; 
    $charArray = str_split($a); 
    $temp = $charArray[$i] ; 
    $charArray[$i] = $charArray[$j]; 
    $charArray[$j] = $temp; 
    return implode($charArray); 
    } 
    // Driver Code 
    $str = "LFC"; 
    $len1 = strlen($str); 
    find_permute($str, 0, $len1 - 1);
    
    // C program to print all permutations with duplicates allowed 
    #include <stdio.h>
    #include <string.h>
    /* Function to swap values at two pointers */
    void swap(char *num1, char *num2) 
    { 
    char temp; 
    temp = *num1; 
    *num1 = *num2; 
    *num2 = temp; 
    } 
    void find_permute(char *a, int len1, int len2) 
    { 
    int i; 
    if (len1 == len2) 
    printf("%s\n", a); 
    else
    { 
    for (i = len1; i <= len2; i++) 
    { 
    swap((a+len1), (a+i)); 
    find_permute(a, len1+1, len2); 
    swap((a+len1), (a+i)); //backtrack 
    } 
    } 
    } 
    int main() 
    { 
    char str[] = "LFC"; 
    int len = strlen(str); 
    find_permute(str, 0, len-1); 
    return 0; 
    } 
    

    Output

    LFC
    LCF
    FLC
    FCL
    CFL
    CLF
    

    Recommended Programs

       Program to sort the characters in a given string.
       Program to check whether string is pangram or not.