🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
A permutation of a string is a rearrangement of its characters. For example, the permutations of the string "ABC" are "ABC", "ACB", "BAC", "BCA", "CAB", and "CBA". This guide will show you how to create a Java program that finds and displays all permutations of a given string.
Problem Statement
Create a Java program that:
- Takes a string as input.
- Finds and displays all the permutations of that string.
Example 1:
- Input:
"ABC" - Output:
ABC, ACB, BAC, BCA, CAB, CBA
Example 2:
- Input:
"AB" - Output:
AB, BA
Solution Steps
- Prompt for Input: Use the
Scannerclass to read a string input from the user. - Create a Recursive Method: Use a recursive method to generate all permutations of the string.
- Display Each Permutation: Print each permutation as it is generated.
Java Program
Java Program to Find All Permutations of a String
import java.util.Scanner;
/**
* Java Program to Find All Permutations of a String
* Author: https://www.javaguides.net/
*/
public class StringPermutations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Prompt the user for input
System.out.print("Enter a string to find its permutations: ");
String input = scanner.nextLine();
// Step 2: Generate and display permutations
System.out.println("Permutations of the string are:");
findPermutations(input, "");
}
// Step 3: Recursive method to find permutations
public static void findPermutations(String str, String prefix) {
if (str.isEmpty()) {
// Base case: If the string is empty, print the prefix
System.out.println(prefix);
} else {
for (int i = 0; i < str.length(); i++) {
// Choose the character at index i
char ch = str.charAt(i);
// Form the remaining string after removing the character at index i
String remaining = str.substring(0, i) + str.substring(i + 1);
// Recurse with the remaining string and the prefix updated with the chosen character
findPermutations(remaining, prefix + ch);
}
}
}
}
Explanation
- Input: The program prompts the user to enter a string.
- Recursive Permutation Generation: The
findPermutationsmethod generates all permutations of the input string by using recursion. The base case is when the input string is empty, at which point the accumulated prefix (which is a permutation) is printed. In the recursive case, the method iterates over the string, removing one character at a time and recursing on the remaining string. - Output: Each permutation is printed as it is generated.
Output Example
Example 1:
Enter a string to find its permutations: ABC
Permutations of the string are:
ABC
ACB
BAC
BCA
CAB
CBA
Example 2:
Enter a string to find its permutations: AB
Permutations of the string are:
AB
BA
Example 3:
Enter a string to find its permutations: A
Permutations of the string are:
A
Example 4:
Enter a string to find its permutations:
Permutations of the string are:
Conclusion
This Java program generates all permutations of a given string using a recursive approach. By systematically removing one character at a time and recursing on the remaining characters, the program effectively generates and displays all possible permutations. This method is elegant and powerful, making it a common choice for generating permutations in programming tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment