1

I have to print all the possible permutations of the given input string.

Using the code below I get aaaa bbb ccc now in next iteration I want to print aaa aab aac. aba aca and so on. Please guide me about it.

String s = "abc";
char ch;
ArrayList<Character> input = new ArrayList<Character>();

public static void main (String [] args)
{
  String  s= "abc";
  int count ; 
  char ch;
  ArrayList<Character> input = new ArrayList<Character>();
  for (int i=0; i < s.length(); i++)
  {
    ch = s.charAt(i);
    input.add(ch);         
  }       


  for (int i=0; i <= input.size(); i++)
  {
    for(int j=0; j < input.size(); j++)
    {
      System.out.print(input.get(i));
    } 
    System.out.println();
  } 
}
14
  • 7
    Why are you using an ArrayList<Character> to store String characters while every character can be read using s.charAt(i)? Commented May 1, 2013 at 9:14
  • @BackSlash i have placed specific string here , but input is not specific that can be abc123 etc . Commented May 1, 2013 at 9:15
  • And what exactly is the desired pattern? Commented May 1, 2013 at 9:16
  • 1
    Sounds like you want every possible permutation of the input characters. Commented May 1, 2013 at 9:33
  • 1
    possible duplicate of Generating all permutations of a given string Commented May 1, 2013 at 9:44

3 Answers 3

3

You can use recursive function. Example

private static String text = "abcd";

public static void main(String[] args) {
    loopPattern(text, 0, "");
}

private static void loopPattern(String source, int index, String res) {
    if (source == null || source.length() == 0) {
    return;
    }
    if (index == source.length()) {
        System.out.println(res);
        return;
    }
    for (int i = 0; i < source.length(); i++) {
        loopPattern(source, index + 1, res + source.charAt(i));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

A recursive version which is not dependent of the number of characters:

class Test
{
    public static void main(String[] args)
    {
        if(args.length != 1)
        {
            System.out.println("Usage: java Test <string>");
            System.exit(1);
        }

        String input = args[0];

        iterate(input, "", input.length());
    }

    public static void iterate(String input, String current, int level)
    {
        if(level == 0)
        {
            System.out.println(current);
            return;
        }

        for(int i=0; i < input.length(); i++)
        {
            iterate(input, current + input.charAt(i), level-1);
        }
    }

Comments

1

In current implementation, you should use:

for (int i=0; i <= input.size(); i++) {
   for(int j=0; j < input.size(); j++) {
      for(int k=0; k < input.size(); k++) {
         System.out.print(input.get(i));
         System.out.print(input.get(j));
         System.out.print(input.get(k));
         System.out.println();
      }
   }
}

But IMHO it is better to use s.charAt(i) instead of input.get(i).

3 Comments

It sounds like the OP wants every possible permutation of the input characters. In which case, this solution doesn't scale because it requires one for loop per character.
this is very much related to what i was told in class , i just did not try it same time and totally lost the idea . this is really helpful
dude this is not the correct solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.