Permutations with Fixed Character Position
This program generates permutations of a string with one character fixed at a position.
Problem Statement
Write a Java program to generate permutations of "ABC" with 'A' fixed at first position.
Source Code
| 1 | public class PermutationsFixedPosition { |
| 2 | public static void permute(String str, String ans){ |
| 3 | if(str.length()==0){ |
| 4 | System.out.println("A"+ans); |
| 5 | return; |
| 6 | } |
| 7 | for(int i=0;i<str.length();i++){ |
| 8 | char ch=str.charAt(i); |
| 9 | String ros=str.substring(0,i)+str.substring(i+1); |
| 10 | permute(ros,ans+ch); |
| 11 | } |
| 12 | } |
| 13 | public static void main(String[] args) { |
| 14 | System.out.println("Eduinq Permutations Fixed Position"); |
| 15 | permute("BC",""); |
| 16 | } |
| 17 | } |
Program Output
Eduinq Permutations Fixed Position ABC ACB
Explanation
The character 'A' is fixed at the first position outside the recursive process, and only the remaining characters BC are permuted recursively, together generating permutations with a constrained fixed position.