recursive call for getting permutations of the string. - Java java.lang

Java examples for java.lang:String Algorithm

Description

recursive call for getting permutations of the string.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String firstString = "abc";
        String secondString = "cba";
        permut(firstString, secondString);
    }/*from  w  ww .  j  a  v a  2 s  .co m*/

    /***
     * This is the recursive call for getting permutations of the string.
     * @param firstString
     * @param secondString
     */
    private static void permut(String firstString, String secondString) {
        if (secondString.length() == 0) {
            System.out.println(firstString);
        } else
            for (int i = 0; i < secondString.length(); i++) {
                permut(firstString + secondString.charAt(i),
                        secondString.substring(0, i)
                                + secondString.substring(i + 1,
                                        secondString.length()));
            }
    }
}

Related Tutorials