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:
Java Program to Perform Naive String Matching
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Hướng dẫn Java Design Pattern – Decorator
Java Program to Check whether Graph is a Bipartite using DFS
Spring RequestMapping
Java Program to Implement Min Hash
Lớp Collectors trong Java 8
Java – InputStream to Reader
Java 14 Record Keyword
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Composition, Aggregation, and Association in Java
How to Read a Large File Efficiently with Java
Jackson – Unmarshall to Collection/Array
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Evaluate an Expression using Stacks
Partition a List in Java
Lập trình đa luồng với CompletableFuture trong Java 8
Hướng dẫn sử dụng lớp Console trong java
Weak References in Java
An Intro to Spring Cloud Vault
Java Program to Construct K-D Tree for 2 Dimensional Data
Hướng dẫn Java Design Pattern – Visitor
Spring Cloud Series – The Gateway Pattern
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Java Program to Implement LinkedBlockingQueue API
Java Program to Implement Gauss Seidel Method
Spring Boot - Runners
Removing Elements from Java Collections
Java Program to Implement Johnson’s Algorithm
Spring Boot - Thymeleaf
Java 8 and Infinite Streams