fork download
  1. import java.util.SortedSet ;
  2. import java.util.TreeSet ;
  3. import java.util.Scanner ;
  4. import java.util.Arrays ;
  5.  
  6. /**
  7.  *
  8.  * @author Ashu
  9.  * Recursive method to find all permutations of a String without any duplicates
  10.  * (if exists).
  11.  */
  12. public final class PermutationRecursive
  13. {
  14. private SortedSet <String> set = new TreeSet <String> () ;
  15.  
  16. private void permuteString(String begStr, String endStr)
  17. {
  18. int length = endStr.length() ;
  19. if (length == 1) set.add(begStr + endStr) ;
  20. else
  21. {
  22. for (int i = 0 ; i < length ; i++)
  23. {
  24. String newStr = endStr.substring(0, i) + endStr.substring(i+1) ;
  25. permuteString(begStr + endStr.charAt(i), newStr) ;
  26. }
  27. }
  28. }
  29.  
  30. private void print(SortedSet <String> list)
  31. {
  32. int k = 0 ;
  33. for (String str : list)
  34. {
  35. System.out.println(++k + " --> " + str) ;
  36. }
  37. System.out.println("Frequency of Permutations : " + k) ;
  38. }
  39.  
  40. private void search(SortedSet <String> list, String sch)
  41. {
  42. int loc = Arrays.binarySearch(list.toArray(), sch) + 1 ;
  43. System.out.println("String Found At :: " + loc) ;
  44. }
  45.  
  46. public static void main(String[] args)
  47. {
  48. PermutationRecursive pR = new PermutationRecursive() ;
  49. Scanner scan = new Scanner(System.in) ;
  50.  
  51. System.out.print("Enter a String :: ") ;
  52. String str = scan.nextLine() ;
  53.  
  54. pR.permuteString("", str) ;
  55. pR.print(pR.set) ;
  56.  
  57. System.out.print("Enter the String to be searched :: ") ;
  58. String input = scan.nextLine() ;
  59. scan.close() ;
  60. pR.search(pR.set, input) ;
  61. }
  62. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Hello
compilation info
Main.java:12: class PermutationRecursive is public, should be declared in a file named PermutationRecursive.java
public final class PermutationRecursive
             ^
1 error
stdout
Standard output is empty