Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
public class Ways {
    public static Set<String> permutationFinder(String str) {
        Set<String> permutation = new HashSet<String>();
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            permutation.add("");
            return permutation;
        }
        char initial = str.charAt(0);
        String rem = str.substring(1);
        Set<String> words = permutationFinder(rem);
        for (String letter : words) {
            for (int i = 0; i <= letter.length(); i++) {
                permutation.add(charInsert(letter, initial, i));
            }
        }
        return permutation;
    }
    public static String charInsert(String str, char c, int j) {
        String a = str.substring(0, j);
        String b = str.substring(j);
        return a + b;
    }
    public static void main(String[] args) {
        String s = "AAC";
        String s1 = "ABC";
        System.out.println(permutationFinder(s));
        System.out.println(permutationFinder(s1));
    }
}


[AAC, ACA, CAA]
[ACB, ABC, BCA, CBA, CAB, BAC]
[AA, A, CA, CAA]
[A, CBA, CA, BA]
[AA, A, CA, CAA]
[A, CBA, CA, BA]
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Collection Framework In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App