-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllPermutationsOfString.java
46 lines (43 loc) · 1.07 KB
/
AllPermutationsOfString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* All permutations of a given String
* This algorithm taken O(n!) time since there are n! permutations possible */
package codingProblems;
import java.util.ArrayList;
import java.util.Scanner;
public class AllPermutationsOfString
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to find permutations:");
System.out.println("All permutations:"+getPerm(in.next()));
in.close();
}
public static ArrayList<String> getPerm(String s)
{
ArrayList<String> perm = new ArrayList<String>();
if (s==null)
return null; //Error condition
else if(s.length() == 0) //base case
{
perm.add("");
return perm;
}
char c=s.charAt(0);
String remainder = s.substring(1);
ArrayList<String> words= getPerm(remainder);
for(String word:words)
{
for(int i=0;i<= word.length();i++)
{
perm.add(insertCharAt(word, c, i));
}
}
return perm;
}
public static String insertCharAt(String s, char c, int i)
{
String a=s.substring(0,i);
String b=s.substring(i);
return a+c+b;
}
}