Print all permutation of a string using ArrayList
Last Updated :
17 Feb, 2023
Given a string str, the task is to print all the permutations of str.
A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement.
For instance, the words ‘bat’ and ‘tab’ represents two distinct permutation (or arrangements) of a similar three-letter word.
Examples:
Input: str = “abc”
Output: abc acb bac bca cba cab
Input: str = “bat”
Output: bat bta abt atb tba tab
Approach: Write a recursive function that will generate all the permutations of the string. Terminating condition will be when the passed string is empty, in that case the function will return an empty ArrayList.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h> // includes all standard libraries in one header
using namespace std;
void printVector(vector<string> arrV)
{
arrV.erase( remove (arrV.begin(), arrV.end(), "" ), arrV.end());
for ( int i = 0; i < arrV.size(); i++)
cout << arrV[i] << " " ;
}
vector<string> getPermutation(string str)
{
if (str.length() == 0) {
vector<string> empty;
empty.push_back( "" );
return empty;
}
char ch = str[0];
string subStr = str.substr(1);
vector<string> prevResult = getPermutation(subStr);
vector<string> Res;
for (string val : prevResult) {
for ( int i = 0; i <= val.length(); i++) {
Res.push_back(val.substr(0, i) + ch + val.substr(i));
}
}
return Res;
}
int main()
{
string str = "abc" ;
printVector(getPermutation(str));
return 0;
}
|
Java
import java.util.ArrayList;
public class GFG {
static void printArrayList(ArrayList<String> arrL)
{
arrL.remove( "" );
for ( int i = 0 ; i < arrL.size(); i++)
System.out.print(arrL.get(i) + " " );
}
public static ArrayList<String> getPermutation(String str)
{
if (str.length() == 0 ) {
ArrayList<String> empty = new ArrayList<>();
empty.add( "" );
return empty;
}
char ch = str.charAt( 0 );
String subStr = str.substring( 1 );
ArrayList<String> prevResult = getPermutation(subStr);
ArrayList<String> Res = new ArrayList<>();
for (String val : prevResult) {
for ( int i = 0 ; i <= val.length(); i++) {
Res.add(val.substring( 0 , i) + ch + val.substring(i));
}
}
return Res;
}
public static void main(String[] args)
{
String str = "abc" ;
printArrayList(getPermutation(str));
}
}
|
Python3
from typing import List
def get_permutation(string: str ) - > List [ str ]:
if len (string) = = 0 :
return [""]
ch = string[ 0 ]
sub_str = string[ 1 :]
prev_result = get_permutation(sub_str)
res = []
for val in prev_result:
for i in range ( len (val) + 1 ):
res.append(val[:i] + ch + val[i:])
return res
def main():
string = "abc"
print (get_permutation(string))
if __name__ = = "__main__" :
main()
|
C#
using System.Collections.Generic;
using System;
class GFG
{
static void printArrayList(List<String> arrL)
{
arrL.Remove( "" );
for ( int i = 0; i < arrL.Count; i++)
Console.Write(arrL[i] + " " );
}
public static List<String> getPermutation(String str)
{
if (str.Length == 0)
{
List<String> empty = new List<String>();
empty.Add( "" );
return empty;
}
char ch = str[0];
String subStr = str.Substring(1);
List<String> prevResult = getPermutation(subStr);
List<String> Res = new List<String>();
foreach (String val in prevResult)
{
for ( int i = 0; i <= val.Length; i++)
{
Res.Add(val.Substring(0, i) + ch + val.Substring(i));
}
}
return Res;
}
public static void Main(String[] args)
{
String str = "abc" ;
printArrayList(getPermutation(str));
}
}
|
Javascript
<script>
function printArrayList(arrL)
{
for (let i = 0; i < arrL.length; i++)
document.write(arrL[i] + " " );
}
function getPermutation(str)
{
if (str.length == 0) {
let empty = [];
empty.push( "" );
return empty;
}
let ch = str[0];
let subStr = str.substring(1);
let prevResult = getPermutation(subStr);
let Res = [];
for (let val of prevResult) {
for (let i = 0; i <= val.length; i++) {
Res.push(val.substring(0, i) + ch + val.substring(i));
}
}
return Res;
}
let str = "abc" ;
printArrayList(getPermutation(str));
</script>
|
Output
abc bac bca acb cab cba
Time Complexity: O(n*n!)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...