-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations_of_string.java
More file actions
41 lines (36 loc) · 964 Bytes
/
Copy pathpermutations_of_string.java
File metadata and controls
41 lines (36 loc) · 964 Bytes
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
import java.util.*;
public class Perm {
public static ArrayList<String> getPerms(String str)
{
if (str==null)
return null;
ArrayList<String> permutations = new ArrayList<String>();
if(str.length() ==0){permutations.add(""); return permutations;}
char first = str.charAt(0);
String remainder = str.substring(1);
ArrayList<String> words = getPerms(remainder);
System.out.println(words);
for(String word : words)
{
for(int j=0;j<=word.length();j++){
String s = insertCharAt(word,first,j);
permutations.add(s);
}
}
System.out.println(permutations);
return permutations;
}
public static String insertCharAt(String word, char c ,int i)
{
String start = word.substring(0,i);
String end= word.substring(i);
return start+c+end;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
ArrayList<String> o = getPerms(s);
System.out.println(o);
}
}