-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutation on string.java
More file actions
38 lines (31 loc) · 971 Bytes
/
Permutation on string.java
File metadata and controls
38 lines (31 loc) · 971 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
/*
Given a string,find it's all permutation using recursion
String = "abc"
all permutation = [abc,acb,bac,bca,cab,cba]
*/
package com.rishabh.recursion;
import java.util.ArrayList;
public class PermutationString {
public static void main(String[] args) {
String str = "abc";
System.out.println(permutaionstring(str));
}
public static ArrayList<String> permutaionstring(String str){
if(str.length() == 0){
ArrayList<String> br = new ArrayList<>();
br.add("");
return br;
}
char ch = str.charAt(0);
String ros = str.substring(1);
ArrayList<String> res = permutaionstring(ros);
ArrayList<String> mr = new ArrayList<>();
for(String val : res){
for (int i = 0; i <= val.length(); i++) {
String s = val.substring(0,i) + ch + val.substring(i);
mr.add(s);
}
}
return mr;
}
}