One fascinating problem in the world of strings is the generation of permutations. In essence, a permutation of a string is any arrangement of its characters in a different sequence. For instance, the permutations of the string "abc" are "abc", "acb", "bac", "bca", "cab", and "cba". In this article, we will write a Java program that generates all permutations of a given string.
The Strategy
- We'll approach this problem recursively.
- Choose each character as a starting character and find permutations of the remaining characters.
- Recursively repeat the process for the remaining characters until no characters are left.
Java Program to Find All the Permutations of a String
public class StringPermutations {
public static void main(String[] args) {
String input = "abc";
generatePermutations(input, "");
System.out.println("All permutations of " + input + " have been displayed.");
}
public static void generatePermutations(String str, String ans) {
if (str.length() == 0) {
System.out.println(ans);
return;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
String remaining = str.substring(0, i) + str.substring(i + 1);
generatePermutations(remaining, ans + ch);
}
}
}
Output:
abc
acb
bac
bca
cab
cba
All permutations of abc have been displayed.
Step by Step Explanation:
Setting Up the String:
String input = "abc";
We initialized a string named input which we want to generate permutations for.
Base Case:
if (str.length() == 0) {
System.out.println(ans);
return;
}
Our recursive function, generatePermutations, will work by picking characters from str and adding them to ans. The base case is when str becomes empty, meaning we have formed a permutation in ans, which we then print.
Recursive Permutations Generation:
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
String remaining = str.substring(0, i) + str.substring(i + 1);
generatePermutations(remaining, ans + ch);
}
This loop traverses through each character in the string. For every character:
- We pick it out (denoted by ch).
- Generate a new string remaining which is the original string minus the picked character.
- We call our function recursively with this new string, adding our chosen character to the ans string.
Recursive Magic:
This process will keep repeating until every character has been the starting character, and we've explored all possible combinations of the remaining characters.
Conclusion
Generating permutations of a string is a classic example of a problem that can be elegantly and efficiently solved using recursion. This beginner's guide provides a clear pathway to understanding and implementing this solution in Java. Remember, recursion can initially feel a little tricky, but with practice, its patterns and beauty will become evident.
Related Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
Comments
Post a Comment
Leave Comment