logo
  • Programming
  • Testing
  • AI
  • Devops
  • Data Science
  • Design
  • Blogs
  • Crypto Tools
  • Dev Feed
  • Login

Visit Channel

Story
Follow @devglan

Java program to Find Permutations of a Given String

author-image   By Dhiraj Ray,   Last updated on: 29 May, 2020 19K

Write a java program to find all the permutations of any given string. Permutation is the each of several possible ways in which a set or number of things can be ordered or arranged. Order matters in case of Permutation. For example, the permutation of ab will be ab and ba. A string of length n can have a permutations of n!. Following is the java program to find permutation of a given string.

The approach would be to take out the first char and keep it constant and permute the rest of the characters and use recursion to permute the rest of the string except the first character. While making the recursive call, we accumulate each character being constant along with recursive call response.

StringPermutations.java
package com.devglan;

import java.util.ArrayList;
import java.util.List;

public class StringPermutations {

    public List getPermutations(String input) {

        List strList = new ArrayList<>();
        permutations("", input, strList);
        return strList;
    }

    public void permutations(String consChars, String input, List list) {

        if(input.isEmpty()) {
            list.add(consChars + input);
            return;
        }

        for(int i = 0; i < input.length(); i++) {
            permutations(consChars + input.charAt(i),
                    input.substring(0, i)+input.substring(i+1), list);
        }
    }

    public static void main(String a[]) {
        StringPermutations permutations = new StringPermutations();
        List output = permutations.getPermutations("india");
        System.out.println("Result size: "+output.size());
        output.stream().forEach(System.out::println);
    }


}

Explanation

Click to Suggest Your Own Explanation

As discussed above, the approach would be to take out the first char and keep it constant and permute the rest of the characters. Assuming abc as the input, the for-each loop equals the length of String. For i = 0, the first constant char would be a and then we get into recursion for "" + bc(input.substring(0, i)+input.substring(i+1)).

Similarly, for i = 1, thew char at index 1(b) will be constant and then we get into recursion with a + c(input.substring(0, i)+input.substring(i+1)).

Below is the recursion tree for abc.

string-permutation-recursive-call

Other Similar Java Programs:

1 Java Program to test if given number is Armstrong or not
2 Java Program to test if a given number is Fibonacci or not
3 java program to find distinct word list from a file
4 Java program to find duplicate character from a string
5 Java Program to find middle index of array where both ends sum is equal
6 Java Program to find line with max character length in descending order in Java
7 Java Program to find max two numbers in an array
8 Java program to find max repeated words from a file
9 Java program to find sum of prime numbers
10 Java program to reverse a given number
11 Java program to reverse a given string
12 Java program to find factorial of a given number
13 Java Program for Binary Search
14 Java Program to Add Two 2D Matrix
15 3 Ways to Check if Given Words are Anagram or not
16 Java Program to Find LCM of a Two Given Number
17 Check Given String is Rotation of Another String
18 Java Program To Check If A Given Number is A Perfect Number
19 Remove Common Characters From Given Strings
20 Find Second Largest Number in Array
21 Java Program To Find the Longest Palindrome Present in a String
22 Java Program to Reverse an Array in Place Without Using Any Second Array
23 Java Program to Print 1 To 10 Without Using Loop
24 Write a Java Program to Compare Files in Java
25 Java Program to Find missing Number in an Array
26 Java Program to Find First non Repeated Character in a String
27 Write a Java Program to Find Union and Intersection of Arrays in Java
28 Writing a Java program to rotate an array by d elements.
29 Write a Java program to rotate a matrix
30 Write a Java program to find the largest sum of the contiguous subarray in a given Array

If You Appreciate This, You Can Consider:

  • Share this article on social media or with your teammates.

Suggest Explanation

The suggestion has been saved for review.Thanks for your effort.

{{errorMessage}}

Vertex

Devglan is one stop platform for all
programming tutorials and courses.

About Us

  • About Us
  • Contact Us
  • Submission Criteria
  • Privacy Policy

Quick Links

  • Home
  • Login / Join
  • Submit Your Story
  • Donate

Contact Us

Dhiraj
dhiraj@devglan.com

© 2021 Devglan. All rights reserved.