
- 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 permutations of a string in Java
Following is the Java program to print all permutations of a string −
Example
public class Demo{ static void print_permutations(String my_str,String my_ans){ if (my_str.length() == 0){ System.out.print(my_ans + " "); return; } boolean my_arr[] = new boolean[26]; for (int i = 0; i < my_str.length(); i++){ char ch = my_str.charAt(i); String remaining_str = my_str.substring(0, i) + my_str.substring(i + 1); if (my_arr[ch - 'a'] == false) print_permutations(remaining_str, my_ans + ch); my_arr[ch - 'a'] = true; } } public static void main(String[] args){ String my_str = "hey"; System.out.println("The permutation of the string are :"); print_permutations(my_str, ""); } }
Output
The permutation of the string are : hey hye ehy eyh yhe yeh
A class named Demo contains a static function ‘print_permutations’, which checks if a string is empty, and if it is, then the output is printed. Now, a Boolean array named ‘my_arr’ is assigned with a size of 36, wherein 'false' values are stored by default. Whenever an alphabet is used, its index in the array is changed to 'true'.
A 'for' loop is used to iterate over the length of the string and the ith character of the string is checked. The remaining part of the string without the ith character is assigned to a string named 'remaining_str'. If the character is not used, a recursive call to the function occurs. Otherwise, no function call occurs. In the main function, a string is defined and the function is called on this string.
- Related Articles
- Print all permutations of a given string
- Print all palindrome permutations of a string in C++
- Python Program to print all permutations of a given string
- C Program to print all permutations of a given string
- Java Program to print distinct permutations of a string
- Print all distinct permutations of a given string with duplicates in C++
- 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 the palindromic permutations of given string in alphabetic order in C++
- All permutations of a string using iteration?
- Print all permutations with repetition of characters in C++
- Creating all possible unique permutations of a string in JavaScript
- Print all permutation of a string using ArrayList in Java
- Print all permutations in sorted (lexicographic) order in C++
- Print first n distinct permutations of string using itertools in Python
