Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Shamir Yona
Added: Sep 2, 2018 1:42 PM
Modified: Sep 2, 2018 2:33 PM
Views: 8
Tags: permutations
  1. // Based on https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8. package StringTests;
  9.  
  10. import java.util.Scanner;
  11.  
  12. /**
  13.  *
  14.  * @author shayzukerman
  15.  */
  16. public class StringPermutations {
  17.  
  18.     public static int permNr = 0;
  19.  
  20.     public static void genPerms(String str) {
  21.         perms(str, "");
  22.     }
  23.  
  24.     private static void perms(String str, String fixed) {
  25.         //System.err.println("perms - str: \"" + str + "\" fixed: \"" + fixed + "\"");
  26.         // Scan the input string and swap the first character with each one
  27.         // of the other characters:
  28.         for (int i = 0; i < str.length(); i++) {
  29.             str = swap(str, 0, i);
  30.             //System.out.println(fixed + str);
  31.  
  32.             perms(str.substring(1), fixed + str.charAt(0));
  33.         }
  34. //        if (str.length() == 0) {
  35. //            System.out.print(fixed + " ");
  36. //        }
  37.        
  38.         if (str.length() == 0) {
  39.             System.out.println((++permNr) + " - " + fixed);
  40.         }
  41.  
  42.     }
  43.  
  44.     private static String swap(String str, int off1, int off2) {
  45.         if (off1 == off2 || off1 < 0 || off1 > str.length() || off2 < 0 || off2 > str.length()) {
  46.             return (str);
  47.         }
  48.  
  49.         if (off1 > off2) {
  50.             int tmp = off2;
  51.             off2 = off1;
  52.             off1 = tmp;
  53.         }
  54.         char[] charArr = str.toCharArray();
  55.         char tmp = charArr[off1];
  56.         charArr[off1] = charArr[off2];
  57.         charArr[off2] = tmp;
  58.        
  59. //        return (str.substring(0, off1) + str.charAt(off2) + str.substring(off1 + 1, off2) + str.charAt(off1) + str.substring(off2 + 1));
  60.         return(new String(charArr));
  61.     }
  62.  
  63.     public static void readTest() {
  64.         try {
  65.             try (Scanner scanner = new Scanner(System.in)) {
  66.                 int numStr = scanner.nextInt();
  67.                 String[] inpArr = new String[numStr];
  68.  
  69.                 for (int i = 0; i < numStr; i++) {
  70.                     inpArr[i] = scanner.next();
  71.                 }
  72.                 for (int i = 0; i < numStr; i++) {
  73.                     genPerms(inpArr[i]);
  74.                     System.out.println();
  75.                 }
  76.  
  77.             }
  78.         } catch (Exception ex) {
  79.             System.err.printf("Exception: %s -%s \n", ex.getClass(),
  80.                     ex.getMessage());
  81.         }
  82.  
  83.     }
  84.  
  85.     public static void main(String[] args) {
  86.         //readTest();
  87.         genPerms("ABSG");
  88.     }
  89. }
  90.