SHARE
TWEET

Untitled

a guest Apr 25th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package pachet1;
  2.  
  3. public class Permutation {
  4.     public static void main(String[] args)
  5.     {
  6.         String str = "ABCDEF";
  7.         Permutation permutation = new Permutation();
  8.         System.out.println(permutation.permute(str, 100, 1));
  9.     }
  10.  
  11.     public String permute(String str, int times, int rightDirection) {
  12.         if(times < 1 || times > 100) {
  13.             throw new IllegalArgumentException("times should be between 1 and 100");
  14.         }
  15.  
  16.         if(str.length() == 0) {
  17.             throw new IllegalArgumentException("String should not be empty");
  18.         }
  19.  
  20.         if(str.length() > 100) {
  21.             throw new IllegalArgumentException("String should have maximum 100 chars.");
  22.         }
  23.  
  24.         if(!(rightDirection == 0 || rightDirection == 1)) {
  25.             throw new IllegalArgumentException("Right can be only 0 or 1");
  26.         }
  27.  
  28.         if (rightDirection == 1) {
  29.             return permute_right(str, times);
  30.         }
  31.         else {
  32.             return permute_left(str, times);
  33.         }
  34.     }
  35.  
  36.     private String permute_right(String str, int times){
  37.         char[] stringChars = str.toCharArray();
  38.  
  39.         for (int i = 0; i < times; i++) {
  40.             char last = stringChars[stringChars.length - 1];
  41.             for(int j = stringChars.length - 2; j >= 0; j--) {
  42.                 stringChars[j + 1] = stringChars[j];
  43.             }
  44.             stringChars[0] = last;
  45.         }
  46.  
  47.         return String.valueOf(stringChars);
  48.     }
  49.  
  50.     private String permute_left(String str, int times){
  51.         char[] stringChars = str.toCharArray();
  52.  
  53.         for (int i = 0; i < times; i++) {
  54.             char first = stringChars[0];
  55.             for(int j = 1; j < stringChars.length; j++) {
  56.                 stringChars[j - 1] = stringChars[j];
  57.             }
  58.             stringChars[stringChars.length - 1] = first;
  59.         }
  60.  
  61.         return String.valueOf(stringChars);
  62.     }
  63.  
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top