Skip to content

Commit a25e2fd

Browse files
committed
return string permu
1 parent c5f9ad1 commit a25e2fd

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
public class Recursion2_ReturnPermutationsString {
3+
public static String[] permutationOfString(String input){
4+
// Write your code here
5+
6+
//base case
7+
if (input.length()==0){
8+
String ans[] = {""};
9+
return ans;
10+
}
11+
12+
//take first character and call recursion on the rest
13+
String smallAns[] = permutationOfString(input.substring(1));
14+
15+
//create new answer array with required number of positions
16+
String ans[] = new String[smallAns.length * input.length()];
17+
18+
//add the first character at every position of the obtained permutations of the substring
19+
int k = 0;
20+
//loop over arrays
21+
for(int i = 0; i<smallAns.length;i++){
22+
//loop over strings
23+
for(int j = 0; j<input.length(); j++){
24+
//build each string as string = before + character + after
25+
ans[k] = smallAns[i].substring(0,j) + input.charAt(0) + smallAns[i].substring(j,smallAns[i].length());
26+
k++;
27+
}
28+
}
29+
30+
return ans;
31+
32+
}
33+
34+
}

0 commit comments

Comments
 (0)