Advertisement
GeorgeColon

StringPerm

Nov 20th, 2017
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package StringPerm;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /*
  6.  * Name: George Colon
  7.  * Date: 11/20/2017
  8.  * Course Number: CSC 111
  9.  * Course Name: Data structures
  10.  * Email: Gcolon6085@student.stcc.edu
  11.  */
  12.  
  13. import java.util.Scanner;
  14.  
  15. public class StringPermHomeWork {
  16.    
  17.    
  18.     //**********************************************
  19.    
  20.     private static void process(Scanner sc, String args[]) {
  21.         // Code here is merely a sample
  22.         String x;
  23.         System.out.print("Please enter a string: ");
  24.         x = sc.next();
  25.         sc.nextLine();  // IMPORTANT!! Reset Scanner
  26.         System.out.println("Processing " + x + " ...");
  27.         displayPermutation(x);
  28.    
  29.     }
  30.    
  31.     //**********************************************
  32.    
  33.     private static boolean doThisAgain(Scanner sc, String prompt) {
  34.         System.out.print(prompt);
  35.         String doOver = sc.nextLine();
  36.         return doOver.equalsIgnoreCase("Y");
  37.     }
  38.    
  39.     //**********************************************
  40.    
  41.     public static void main(String args[]) {
  42.         final String TITLE = "CSC111 String Permutation";
  43.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  44.        
  45.         System.out.println("Welcome to " + TITLE);
  46.         Scanner sc = new Scanner(System.in);
  47.         do {
  48.             process(sc, args);
  49.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  50.         sc.close();
  51.         System.out.println("Thank you for using " + TITLE);
  52.     }
  53.  
  54.     public static void displayPermutation(String s) {
  55.         // using this array list to keep track of already printed outputs.
  56.         // If the char has matching characters this e.g. aaabbc without it we will get
  57.         // outputs multiple times
  58.         // The math of this is 6! / 3!*2! this will give 60 outputs yet without the
  59.         // array list we would have 720.
  60.         ArrayList<String> used = new ArrayList<String>();
  61.         worker("", s, used);
  62.  
  63.     }
  64.  
  65.     public static void worker(String s1, String s2, ArrayList<String> used) {
  66.  
  67.         if (s2.length() == 0 && used.contains(s1) == false) {
  68.             used.add(s1);
  69.             System.out.println(s1);
  70.         } else {
  71.  
  72.             StringBuilder string = new StringBuilder(s2);
  73.             StringBuilder hold = new StringBuilder(s1);
  74.             for (int i = 0; i < string.length(); i++) {
  75.                 char h = string.charAt(i);
  76.                 // Move down a level -Recursively solve-
  77.                 worker(hold.append(h).toString(), string.deleteCharAt(i).toString(), used);
  78.                 // add the removed back in the order they were removed
  79.                 string.insert(i, h);
  80.                 hold.deleteCharAt(hold.length() - 1);
  81.             }
  82.         }
  83.     }
  84.    
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement