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:
New in Spring Security OAuth2 – Verify Claims
Spring Data Java 8 Support
Lớp Properties trong java
Java Program to Perform Deletion in a BST
How to Get All Dates Between Two Dates?
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to Implement Sorted Circularly Singly Linked List
Request Method Not Supported (405) in Spring
Interface trong Java 8 – Default method và Static method
Hướng dẫn Java Design Pattern – Decorator
Java Program to Implement Extended Euclid Algorithm
Apache Commons Collections OrderedMap
Working with Tree Model Nodes in Jackson
Guide to PriorityBlockingQueue in Java
Check if there is mail waiting
Java Program to Implement Coppersmith Freivald’s Algorithm
Spring Boot - Bootstrapping
Allow user:password in URL
Ép kiểu trong Java (Type casting)
Guide to Guava Table
New Features in Java 8
Removing all Nulls from a List in Java
Giới thiệu HATEOAS
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Request a Delivery / Read Receipt in Javamail
Logging a Reactive Sequence
Split a String in Java
Java Program to Implement Radix Sort
Convert Time to Milliseconds in Java
Limiting Query Results with JPA and Spring Data JPA