This is a java program to generate and print all possible combinations out of a, b, c, d, e. The trick here is to start with one letter combinations, then with two letter combinations and so on.
Here is the source code of the Java Program to Generate All Possible Combinations Out of a, b, c, d, e. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to print all possible combinations out of a, b, c, d, e
public class All_Possible_Combinatons
{
static void printCombinations(char[] sequence, int N)
{
char[] data = new char[N];
for (int r = 0; r < sequence.length; r++)
combinations(sequence, data, 0, N - 1, 0, r);
}
static void combinations(char[] sequence, char[] data, int start, int end,
int index, int r)
{
if (index == r)
{
for (int j = 0; j < r; j++)
System.out.print(data[j] + " ");
System.out.println();
}
for (int i = start; i <= end && ((end - i + 1) >= (r - index)); i++)
{
data[index] = sequence[i];
combinations(sequence, data, i + 1, end, index + 1, r);
}
}
public static void main(String args[])
{
char[] sequence = { 'a', 'b', 'c', 'd', 'e' };
System.out.print("The combinations are: ");
printCombinations(sequence, sequence.length);
}
}
Output:
$ javac All_Possible_Combinatons.java $ java All_Possible_Combinatons The combinations are: a b c d e a b a c a d a e b c b d b e c d c e d e a b c a b d a b e a c d a c e a d e b c d b c e b d e c d e a b c d a b c e a b d e a c d e b c d e
Related posts:
Getting the Size of an Iterable in Java
Prevent Cross-Site Scripting (XSS) in a Spring Application
Jackson – Change Name of Field
Java Program to Implement Best-First Search
Apache Commons Collections BidiMap
Build a REST API with Spring and Java Config
REST Web service: Upload và Download file với Jersey 2.x
Hướng dẫn Java Design Pattern – Strategy
Introduction to Spring Data REST
Một số ký tự đặc biệt trong Java
Display Auto-Configuration Report in Spring Boot
Java Program to Implement Hash Trie
How To Serialize and Deserialize Enums with Jackson
“Stream has already been operated upon or closed” Exception in Java
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
A Quick Guide to Using Keycloak with Spring Boot
Java toString() Method
Java Program to Implement Disjoint Sets
How to Return 404 with Spring WebFlux
Hướng dẫn Java Design Pattern – Singleton
Java Program to Compute Cross Product of Two Vectors
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Call Methods at Runtime Using Java Reflection
wait() and notify() Methods in Java
Java Program to Find Nearest Neighbor Using Linear Search
Java Program to Implement RoleUnresolvedList API
Guide to the Volatile Keyword in Java
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Implement Caesar Cypher
Java Program to Implement SimpeBindings API
Spring Security Login Page with React