-1

I'm trying to implement this in Java but I don't how to. Any suggestions?

Examples.

input:

abc

output:

abc
acb
bac
bca
cab
cba

input:

aab

output:

aab
aba
baa

1737973
  • 159
  • 18
  • 42
Fredgrub
  • 35
  • 5
  • 4
    Welcome to StackOverflow. Please show us what you have tried so far, and which part you are finding issue with ? – Om Sao Oct 18 '19 at 15:37
  • For starter: https://www.javatpoint.com/java-program-to-check-whether-two-strings-are-anagram-or-not – Om Sao Oct 18 '19 at 15:37
  • 1
    Possible duplicate of [How to check if two words are anagrams](https://stackoverflow.com/questions/15045640/how-to-check-if-two-words-are-anagrams) – Om Sao Oct 18 '19 at 15:38
  • i don't know how to put my ideias into programming lenguage. – Fredgrub Oct 18 '19 at 16:16

1 Answers1

0

This was the simplest way I could think of

 String str= "abc";
            ArrayList<String> letters = new ArrayList<String>();
            HashSet<String> combinations = new HashSet<String>();
            for(int i = 0; i<str.length();i++) {
//Break String into letters
                letters.add(str.substring(i,i+1));
            }
            for(int i = (str.length()*2)+1;i>0;i--) {
//Will loop through maximum possible outcomes
                String result = "";
                String bin = Integer.toBinaryString(i);
//System.out.println(bin);
                while(bin.length()<3) {
                    bin="0"+bin;
                }
                for(int b = bin.length();b>0;b--) {
                    if(bin.substring(b-1,b).equals("1"))result = result.concat(letters.get(b-1));
                }
                for(int b = bin.length();b>0;b--) {
                    if(bin.substring(b-1,b).equals("0"))result = result.concat(letters.get(b-1));
                }
                combinations.add(result);
            }
            System.out.println(str);
            for(String c : combinations) {
                System.out.println(c);
            }

It uses binary to loop through an array of the original string then adds the result to a hash set to remove duplicates

Tom Chumley
  • 144
  • 5
  • I need to study about ArrayList and HashSet. I've never heard about that. – Fredgrub Oct 18 '19 at 16:19
  • @Fredgrub They are useful for storing and accessing multiple variable. There are some great resources in the JavaDocs as well as GoalKicker if you are interested in learning more – Tom Chumley Oct 18 '19 at 16:21