Print all permutations of a string

Here given code implementation process.

//C Program
//Print all permutations of a string
#include <stdio.h>

//Swap two elements in given string
//i and j is location
void swap(char str[],int i,int j)
{
  char temp=str[i];
  str[i]=str[j];
  str[j]=temp;
}

//Method which is print all permutations of given string
void permutation(char str[],int n ,int size)
{
  
  if(n>size) 
  {
    return;
  }

  if(n==size) 
  {
    printf("%s\n",str );
    return;
  }

  for (int i = n; i < size; ++i)
  {
    //swap the array element
    swap(str,i,n);
    
    permutation(str,n+1,size);

    //swap the array element
    swap(str,i,n);
  }
}


int main()
{
  //Given string is form of array
  char str1[]="ABC";
  //Get the size 
  int size=sizeof(str1)/sizeof(str1[0]);
  permutation(str1,0,size-1);


  printf("\n");
  char str2[]="abcd";
  //Get the size 
  size=sizeof(str2)/sizeof(str2[0]);
  permutation(str2,0,size-1);
  return 0;
}

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
//C++ Program
//Print all permutations of a string
#include<iostream>

using namespace std;
class MyBacktracking {
	public:

    //Swap the string element of given indexes
    string swap(string text,int size,int a,int b)
    {
      if((a>=0 && a<size)  && (b >= 0 && b < size))
      {
        //When valid a and b location
        char temp = text[a];
        text[a] = text[b];
        text[b] = temp;

      }
      //return modified result
      return text;
    }
	//Method which is print all permutations of given string
	void permutation(string str, int n, int size) {
		if (n > size) {
			return;
		}
		if (n == size) {
			cout << str << "\n";
			return;
		}
		for (int i = n; i < size; ++i) {
			//swap the array element
			str = this->swap(str, size, i, n);
			this->permutation(str, n + 1, size);
			//swap the array element
			str = this->swap(str, size, i, n);
		}
	}
};
int main() {
	MyBacktracking obj = MyBacktracking();
	string str = "ABC";
	int size = str.size();
	obj.permutation(str, 0, size);
	cout << "\n";
	str = "abcd";
	size = str.size();
	obj.permutation(str, 0, size);
	return 0;
}

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
//Java Program
//Print all permutations of a string
public class MyBacktracking 
{
  //Swapping two string elements by index
  public String swap(String text,int size,int a,int b)
  {
    //Check valid location of swap element
    if((a>=0 && a<size)  && (b >= 0 && b < size))
    {
      //Get first character
      char first = text.charAt(a);

      //Get second character
      char second = text.charAt(b);

      //Put character
      text = text.substring(0, b) 
                + first 
                + text.substring(b + 1);

      text = text.substring(0, a) 
                + second
                + text.substring(a + 1);
    }

    return text;
  }
  //Method which is print all permutations of given string
  public void permutation(String str, int n, int size) {
    if (n > size) 
    {
      return;
    }
    if (n == size) 
    {
      System.out.print(str+"\n");
      return;
    }
    for (int i = n; i < size; ++i) {
      //swap the array element
      str=swap(str,size, i, n);

      permutation(str, n + 1, size);
      //swap the array element
      str=swap(str,size, i, n);
    }
  }

  public static void main(String[] args) 
  {
    
    MyBacktracking obj = new MyBacktracking();

    String str = "ABC";
    
    int size = str.length();

    obj.permutation(str,0,size);

    System.out.print("\n");
    str = "abcd";

    size = str.length();

    obj.permutation(str,0,size);
  }
}

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
//C# Program
//Print all permutations of a string
using System;
public class MyBacktracking {
	//Swapping two string elements by index
	public String swap(String text, int size, int a, int b) {
		//Check valid location of swap element

		if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
			//Get first character
			char first = text[a];
			//Get second character
			char second = text[b];
			//Put characters
			text = text.Substring(0, b) + first + text.Substring(b + 1);
			text = text.Substring(0, a) + second + text.Substring(a + 1);
		}
      	//important this text are modified inside a function.  
      	//return modified text and assign on actual text variable
		return text;
	}
	//Method which is print all permutations of given string
	public void permutation(String str, int n, int size) {
		if (n > size) {
			return;
		}
		if (n == size) {
			Console.Write(str + "\n");
			return;
		}
		for (int i = n; i < size; ++i) {
			//swap the array element
			str = swap(str, size, i, n);
			permutation(str, n + 1, size);
			//swap the array element
			str = swap(str, size, i, n);
		}
	}
	public static void Main(String[] args) {
		MyBacktracking obj = new MyBacktracking();
		String str = "ABC";
		int size = str.Length;
		obj.permutation(str, 0, size);
		Console.Write("\n");
		str = "abcd";
		size = str.Length;
		obj.permutation(str, 0, size);
	}
}

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
<?php
//Php Program
//Print all permutations of a string
class MyBacktracking {
    //Swapping two string elements by index
    public  function swap($text, $size, $a, $b) {
      //Check valid location of swap element

      if (($a >= 0 && $a < $size) && ($b >= 0 && $b < $size)) {
        //Get first character
        $first = $text[$a];
        //Get second character
        $second = $text[$b];
        //Put character
        $text = substr($text,0, $b-strlen($text)) . $first .substr($text,$b+1 );

        $text = substr($text,0, $a-strlen($text)) . $second .substr($text,$a+1 );
      }
      return $text;
    }


	//Method which is print all permutations of given string
	public 	function permutation($str, $n, $size) {
		if ($n > $size) {
			return;
		}
		if ($n == $size) {
			echo($str ."\n");
			return;
		}
		for ($i = $n; $i < $size; ++$i) {
			//swap the array element
			$str = $this->swap($str, $size, $i, $n);
			$this->permutation($str, $n + 1, $size);
			//swap the array element
			$str = $this->swap($str, $size, $i, $n);
		}
	}
}

function main() {
	$obj = new MyBacktracking();
	$str = "ABC";
	$size = strlen($str);
	$obj->permutation($str, 0, $size);
	echo("\n");
	$str = "abcd";
	$size = strlen($str);
	$obj->permutation($str, 0, $size);

}
main();

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
//Node Js Program
//Print all permutations of a string
class MyBacktracking {
	//Swapping two string elements by index
	swap(text, size, a, b) {
		//Check valid location of swap element

		if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
			//Get first character
			var first = text[a];
			//Get second character
			var second = text[b];
			//Put character
			text = text.substring(0, b) + first + text.substring(b + 1);
			text = text.substring(0, a) + second + text.substring(a + 1);
		}

		return text;
	}
	//Method which is print all permutations of given string
	permutation(str, n, size) {
		if (n > size) {
			return;
		}

		if (n == size) {
			process.stdout.write(str + "\n");
			return;
		}

		for (var i = n; i < size; ++i) {
			//swap the array element
			str = this.swap(str, size, i, n);
			this.permutation(str, n + 1, size);
			//swap the array element
			str = this.swap(str, size, i, n);
		}
	}
}

function main(args) {
	var obj = new MyBacktracking();
	var str = "ABC";
	var size = str.length;
	obj.permutation(str, 0, size);
	process.stdout.write("\n");
	str = "abcd";
	size = str.length;
	obj.permutation(str, 0, size);
}

main();

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
# Python 3 Program
# Print all permutations of a string
class MyBacktracking :
  # Swapping two string elements by index
  def swap(self, text, size, a, b) :
    # Check valid location of swap element
    if ((a >= 0 and a < size) and(b >= 0 and b < size)) :
      data = list(text)
      data[a],data[b] = data[b],data[a]
      return ''.join(data)

    return text
  

  # Method which is print all permutations of given string
  def permutation(self, str, n, size) :
    if (n > size) :
      return
    
    if (n == size) :
      print(str)
      return
    
    i = n
    while (i < size) :
      
      # swap the array element
      str = self.swap(str, size, i, n)
      self.permutation(str, n + 1, size)
      # swap the array element
      str = self.swap(str, size, i, n)
      i += 1
    
    
  

def main() :
  obj = MyBacktracking()
  str = "ABC"
  size = len(str)
  
  obj.permutation(str, 0, size)
  print(end = "\n")
  str = "abcd"
  size = len(str)
  obj.permutation(str, 0, size)


if __name__ == "__main__":
  main()

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
# Ruby Program
# Print all permutations of a string
class MyBacktracking 
	 # Swapping two string elements by index
	def swap(text, size, a, b) 
		 # Check valid location of swap element
		if ((a >= 0 && a < size) && (b >= 0 && b < size)) 
			 # Get first character
			first = text[a]
			text[a]=text[b]
			text[b]=first
		end
		return text
	end
	# Method which is print all permutations of given string
	def permutation(str, n, size) 
		if (n > size) 
			return
		end
		if (n == size) 
			print(str ,"\n")
			return
		end
		i = n
		while (i < size) 
			# swap the array element
			str = self.swap(str, size, i, n)
			self.permutation(str, n + 1, size)
			# swap the array element
			str = self.swap(str, size, i, n)
			i += 1
		end
	end
end
def main() 
	obj = MyBacktracking.new()
	str = "ABC"
	size = str.length()
	obj.permutation(str, 0, size)
	print("\n")
	str = "abcd"
	size = str.length()
	obj.permutation(str, 0, size)
end
main()

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
//Scala Program
//Print all permutations of a string
class MyBacktracking {
	//Swapping two string elements by index
	def swap(info: String, size: Int, a: Int, b: Int): String = {
		//Check valid location of swap element
      	var text = info;
		if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
			//Get first character
			val first: Char = text(a);

			//Get second character
			val second: Char = text(b);

			//Put character
			text = text.substring(0, b) + first + text.substring(b + 1);
			text = text.substring(0, a) + second + text.substring(a + 1);
		}
		return text;
	}
	//Method which is print all permutations of given string
	def permutation(str: String, n: Int, size: Int): Unit = {
		if (n > size) {
			return;
		}
		if (n == size) {
			print(str + "\n");

			return;
		}
		var i: Int = n;
		var new_str = str;
		while (i < size) {
			//swap the array element
			new_str = swap(new_str, size, i, n);
			permutation(new_str, n + 1, size);

			//swap the array element
			new_str = swap(new_str, size, i, n);
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyBacktracking = new MyBacktracking();
		var str: String = "ABC";
		var size: Int = str.length();
		obj.permutation(str, 0, size);
		print("\n");
		str = "abcd";
		size = str.length();
		obj.permutation(str, 0, size);
	}
}

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
//Swift Program
//Print all permutations of a string
class MyBacktracking {
	//Swapping two string elements by index
	func swap(_ text: String, _ size: Int, _ a: Int, _ b: Int) -> String {
		//Check valid location of swap element

		if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
			var result = Array(text)
            result.swapAt(a, b)
            return String(result)
		}
		return text;
	}
	//Method which is print all permutations of given string
	func permutation(_ str: String, _ n: Int, _ size: Int) {
		if (n > size) {
			return;
		}
		if (n == size) {
			print(str ,"\n", terminator: "");
			return;
		}
		var i: Int = n;
      	var new_str = str;
		while (i < size) {
			//swap the array element
			new_str = self.swap(new_str, size, i, n);
			self.permutation(new_str, n + 1, size);
			//swap the array element
			new_str = self.swap(new_str, size, i, n);
			i += 1;
		}
	}
}
func main() {
	let obj: MyBacktracking = MyBacktracking();
	var str: String = "ABC";
	var size: Int = str.count;
	obj.permutation(str, 0, size);
	print("\n", terminator: "");
	str = "abcd";
	size = str.count;
	obj.permutation(str, 0, size);
}
main();

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved