Skip to content

Commit 22329fa

Browse files
committed
Create findAllPermutation.java
1 parent a16d6e6 commit 22329fa

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Utility class to generate and print all permutations of a given string.
3+
*
4+
* <p>
5+
* This class demonstrates a simple recursive algorithm that builds permutations
6+
* by selecting each character in turn and recursing on the remaining characters.
7+
* Each complete permutation is printed to System.out as it is constructed.
8+
* </p>
9+
*
10+
* <p>Behavior:
11+
* <ul>
12+
* <li>The recursive helper {@code findPermutations(String, String)} takes two
13+
* arguments: {@code str} (characters left to permute) and {@code ans}
14+
* (prefix built so far).</li>
15+
* <li>When {@code str} is empty, {@code ans} is a complete permutation and is printed.</li>
16+
* </ul>
17+
* </p>
18+
*
19+
* <p>Complexity:
20+
* <ul>
21+
* <li>Time: O(n * n!) where n is the length of the input string (n! permutations, each of length n printed).</li>
22+
* <li>Space: O(n) additional stack space for recursion depth (not counting output).</li>
23+
* </ul>
24+
* </p>
25+
*
26+
* <p>Notes:
27+
* <ul>
28+
* <li>The implementation prints permutations directly; it does not return a collection.</li>
29+
* <li>If the input contains duplicate characters, duplicate permutations will be produced.</li>
30+
* </ul>
31+
* </p>
32+
*
33+
* <p>Example usage: call {@code findPermutations("ABC", "")} to print:
34+
* {@code ABC, ACB, BAC, BCA, CAB, CBA} (each on its own line).</p>
35+
*/
36+
37+
/**
38+
* Recursively generates and prints all permutations of the characters in {@code str}.
39+
*
40+
* <p>Algorithm:
41+
* For each index {@code i} in {@code str}, the character at {@code i} is chosen
42+
* as the next character in the permutation, removed from {@code str}, appended
43+
* to {@code ans}, and the method recurses on the remaining characters.</p>
44+
*
45+
* @param str the string containing characters yet to be placed in the permutation;
46+
* on each recursive call one character is removed (the chosen character)
47+
* and the remainder is passed down
48+
* @param ans the accumulated prefix (characters already chosen for the current permutation)
49+
*
50+
* <p>Side effects: Prints each complete permutation to standard output via {@code System.out.println}.</p>
51+
*
52+
* <p>Thread-safety: Not thread-safe with respect to output; the method itself uses no shared mutable state.</p>
53+
*/
54+
55+
/**
56+
* Entry point for demonstration.
57+
*
58+
* <p>Constructs an example input string (currently "ABC") and invokes
59+
* {@link #findPermutations(String,String)} to print all permutations to standard output.</p>
60+
*
61+
* @param args command-line arguments (ignored)
62+
*/
63+
public class findAllPermutation {
64+
65+
private static void findPermutations(String str, String ans){
66+
if(str.length() ==0){
67+
System.out.println(ans);
68+
return;
69+
}
70+
for(int i=0;i<str.length();i++){
71+
char ch = str.charAt(i);
72+
String ros = str.substring(0,i) + str.substring(i+1);
73+
findPermutations(ros, ans + ch);
74+
}
75+
}
76+
public static void main(String[] args) {
77+
String str = "ABC";
78+
findPermutations(str, "");
79+
}
80+
}

0 commit comments

Comments
 (0)