
- 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
Java Program to print distinct permutations of a string
To print distinct permutations of a string, the Java program is as follows −
Example
import java.util.ArrayList; public class Demo{ static boolean is_present(String my_str, ArrayList<String> rem){ for (String str : rem){ if (str.equals(my_str)) return true; } return false; } static ArrayList<String> distinct_pattern(String str){ if (str.length() == 0){ ArrayList<String> base_Val = new ArrayList<>(); base_Val.add(""); return base_Val; } char ch = str.charAt(0); String rem_str = str.substring(1); ArrayList<String> prev_str = distinct_pattern(rem_str); ArrayList<String> rem = new ArrayList<>(); for (String my_str : prev_str){ for (int i = 0; i <= my_str.length(); i++){ String f = my_str.substring(0, i) + ch + my_str.substring(i); if (!is_present(f, rem)) rem.add(f); } } return rem; } public static void main(String[] args){ String my_str = "mnqm"; System.out.println("The distinct permutations of the string are "); System.out.println(distinct_pattern(my_str)); } }
Output
The distinct permutations of the string are [mnqm, nmqm, nqmm, mqnm, qmnm, qnmm, mqmn, qmmn, mnmq, nmmq, mmnq, mmqn]
A class named Demo contains a Boolean function named ‘is_present’ that checks to see if the string is actually present. It returns true or false depending on whether the string has some characters or not. Another function named ‘distinct_pattern’ creates an arraylist.
Another string is defined, named ‘rem_str’, that stores the substring of the string. This ‘distinct_function’ is called by passing the ‘rem_str’. In this way, the string is iterated over, and every character’s position is checked, before generating another distinct permutation. This way, all duplicates are avoided. In the end, all the unique permutations are displayed on the console. The main function defines the string, and the function ‘distinct_pattern’ is called on this string. The relevant output is displayed on the console.
- Related Articles
- Print all distinct permutations of a given string with duplicates in C++
- Print all permutations of a string in Java
- Print first n distinct permutations of string using itertools in Python
- Python Program to print all permutations of a given string
- C Program to print all permutations of a given string
- Print all permutations of a given string
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- Print all palindrome permutations of a string in C++
- Java Program to Print a String
- Java program to print all distinct elements of a given integer array in Java
- Print distinct sorted permutations with duplicates allowed in input in C++
- Java Program to Print all unique words of a String
- C++ Program to Find the Number of Permutations of a Given String
- Python program to get all permutations of size r of a string
