fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static void printPermutn(String str, String ans)
  11. {
  12.  
  13. // If string is empty
  14. if (str.length() == 0) {
  15. System.out.print(ans + " ");
  16. return;
  17. }
  18.  
  19. for (int i = 0; i < str.length(); i++) {
  20.  
  21. // ith character of str
  22. char ch = str.charAt(i);
  23.  
  24. // Rest of the string after excluding
  25. // the ith character
  26. String ros = str.substring(0, i) +
  27. str.substring(i + 1);
  28.  
  29. // Recurvise call
  30. printPermutn(ros, ans + ch);
  31. }
  32. }
  33.  
  34. // Driver code
  35. public static void main(String[] args)
  36. {
  37. String s = "XOX";
  38. printPermutn(s, "");
  39. }
  40. }
Success #stdin #stdout 0.14s 36752KB
stdin
Standard input is empty
stdout
XOX XXO OXX OXX XXO XOX