Print Permutations - Question | Recursion | Data Structure and Algorithms in JAVA

preview_player
Показать описание

NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we introduce a new variation of Recursion and state the problem where we are required to print all the permutations for a string. To watch the solution of this problem, click here:

#pepcoding #java #programming
Рекомендации по теме
Комментарии
Автор

Sir, which software you are using for creating this video?

MyDemoChannelForYTD
Автор

Sir I solved this question in two ways

1. using ArrayList
2. without using ArrayList

using Arraylist

package test;

import java.util.*;
public class test {
static ArrayList<String> perm(String n)
{
if(n.length()==0)
{
ArrayList<String> al=new ArrayList<>();
al.add("");
return al;
}
ArrayList<String> al=new ArrayList<>();
for(int i=0;i<n.length();i++)
{
String s1=n.substring(0, i)+n.substring(i+1, n.length());
ArrayList<String> al1=perm(s1);
for(int j=0;j<al1.size();j++)
{

}
}
return al;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter the string ");
String n=s.next();
ArrayList<String> al=perm(n);
for(int i=0;i<al.size();i++)
{

}
System.out.println("Total number of permutation formed "+al.size());
}
}

Without using ArrayList


import java.util.*;
public class AllPermutation10 {
static int count=0;
static void perm(String n, String ans)
{
if(n.length()==0)
{
System.out.println(ans);
count++;
return;
}
for(int i=0;i<n.length();i++)
{
char ch=n.charAt(i);
String s1=n.substring(0, i)+n.substring(i+1, n.length());
perm(s1, ans+ch);
}
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter the string ");
String n=s.next();
perm(n, "");
System.out.println("Total number of permutation formed "+count);
}
}

pushkargoyal
Автор

solved this using faith and expectation

class Solution
{
public:
S){
if(S.empty()){
vector<string> res={""};
return res;
}

vector<string> res;
int n=S.size();

for(int i=0;i<n;i++){
char c = S[i];
vector<string> t=find_permutation(S.erase(i, 1));

for(int j=0;j<t.size();j++)
res.push_back(c + t[j]);

S.insert(i, 1, c);
}

sort(res.begin(), res.end());
return res;
}
};

AnandKumar-kzls
Автор

sir video quality thodi improve ki ja sakti h

itachiuchiha-vsqb