import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StringPermutaion {
//How this class works
//1. Get a string from user
//2. Check for spaces or null
//3. Call permutation function
//4. How permutation works?
//1. take the first element and permutate the rest
//2. call a recursive function to do this
public static void main(String[] args) {
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter the String :");
try {
String input=br.readLine();
if(input !=null && input.length()>0){
StringPermutaion sp=new StringPermutaion();
sp.permutateString("", input);
}
else{
System.out.println("Please enter a valid String");
}
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void permutateString(String s1, String s2){
if(s2.length()<=1){
System.out.println(s1+s2);
}
else{
for(int i=0;i<s2.length();i++){
String s=s2.substring(0,i)+s2.substring(i+1);
permutateString(s1+s2.charAt(i), s);
}
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class StringPermutaion {
//How this class works
//1. Get a string from user
//2. Check for spaces or null
//3. Call permutation function
//4. How permutation works?
//1. take the first element and permutate the rest
//2. call a recursive function to do this
public static void main(String[] args) {
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter the String :");
try {
String input=br.readLine();
if(input !=null && input.length()>0){
StringPermutaion sp=new StringPermutaion();
sp.permutateString("", input);
}
else{
System.out.println("Please enter a valid String");
}
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void permutateString(String s1, String s2){
if(s2.length()<=1){
System.out.println(s1+s2);
}
else{
for(int i=0;i<s2.length();i++){
String s=s2.substring(0,i)+s2.substring(i+1);
permutateString(s1+s2.charAt(i), s);
}
}
}
}
No comments:
Post a Comment