forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringPermutations.java
40 lines (37 loc) · 1.14 KB
/
StringPermutations.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
package me.ramswaroop.strings;
/**
* Created by IntelliJ IDEA.
*
* @author: ramswaroop
* @date: 9/24/15
* @time: 2:27 PM
* @see: http://www.ericleschinski.com/c/java_permutations_recursion/
* @see: http://introcs.cs.princeton.edu/java/23recursion/Permutations.java.html
* @see: me.ramswaroop.strings.StringPermutationCount for a modification of this problem
*/
public class StringPermutations {
/**
* Generates and prints all possible permutations (in order)
* of string {@param s}.
*
* @param prefix
* @param s
*/
public static void printAllPermutations(String prefix, String s) {
int len = s.length();
if (len == 0) {
System.out.println(prefix);
} else {
for (int i = 0; i < len; i++) {
printAllPermutations(prefix + s.charAt(i), s.substring(0, i) + s.substring(i + 1));
}
}
}
public static void main(String a[]) {
printAllPermutations("", "a");
System.out.println("-------");
printAllPermutations("", "ab");
System.out.println("-------");
printAllPermutations("", "abc");
}
}