
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Print all permutation of a string using ArrayList in Java
In this problem, we are given a string of size n and we have to print all permutations of the string. But this time we have to print this permutation using ArrayList.
Let’s take an example to understand the problem -
Input − string = ‘XYZ’
Output − XYZ, XZY, YXZ, YZX, ZXY, ZYX
To solve this problem, we will be generating all permutations of the character of the string. We will use a recursive function and will return arrayList.
Example
The following is ArrayList implementation of the algorithm −
import java.util.ArrayList; public class Main{ static void printArrayList(ArrayList<String> combo) { combo.remove(""); for (int i = 0; i < combo.size(); i++) System.out.print(combo.get(i)+"\t"); } public static ArrayList<String> generatePermutation(String str) { if (str.length() == 0) { ArrayList<String> empty = new ArrayList<>(); empty.add(""); return empty; } char ch = str.charAt(0); String subStr = str.substring(1); ArrayList<String> lastCombination = generatePermutation(subStr); ArrayList<String> newCombination = new ArrayList<>(); for (String val : lastCombination) { for (int i = 0; i <= val.length(); i++) { newCombination.add(val.substring(0, i) + ch + val.substring(i)); } } return newCombination; } public static void main(String[] args) { String str = "NOPQ"; System.out.println("Permutations of string are :"); printArrayList(generatePermutation(str)); } }
Output
Permutations of string are : NOPQ ONPQ OPNQ OPQN NPOQ PNOQ PONQ POQN NPQO PNQO PQNO PQON NOQP ONQP OQNP OQPN NQOP QNOP QONP QOPN NQPO QNPO QPNO QPON
- Related Articles
- Print all subsequences of a string using ArrayList in C++
- Print all permutations of a string in Java
- How to print all the characters of a string using regular expression in Java?
- Print all subsequences of a string using Iterative Method in C++
- Java Program to Print all unique words of a String
- How to find all the permutation of the string by backtracking using C#?
- Convert an ArrayList of String to a String array in Java
- Print all subsequences of a string in C++
- Permutation of a given string using the inbuilt function in Python
- Print all permutations of a given string
- Converting ArrayList to String[] in java
- How can we print all the capital letters of a given string in Java?
- Print all palindromic partitions of a string in C++
- Print all palindrome permutations of a string in C++
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion

Advertisements