Advertisement
HasanRasulov

CountAndRadix.java

Oct 31st, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1.  
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. class Pair {
  7.  
  8.     public byte count;
  9.  
  10.     public List<String> words;
  11.  
  12.     public Pair() {
  13.         words = new LinkedList<>();
  14.         count = 0;
  15.     }
  16. }
  17.  
  18. public class CountAndRadix {
  19.  
  20.     static int max = 0;
  21.  
  22.     static int findLongestString(String... arr) {
  23.  
  24.         int maxLength = 0;
  25.         for (String s : arr) {
  26.             if (s.length() > maxLength) {
  27.                 maxLength = s.length();
  28.             }
  29.         }
  30.         return maxLength;
  31.  
  32.     }
  33.  
  34.     static String[] util(String... arr) {
  35.  
  36.         max = findLongestString(arr);
  37.  
  38.         String res[] = new String[arr.length];
  39.         int i = 0;
  40.         for (String string : arr) {
  41.  
  42.             while (string.length() < max) {
  43.                 string = string.concat("}");
  44.  
  45.             }
  46.  
  47.             res[i++] = string;
  48.         }
  49.         return res;
  50.     }
  51.  
  52.     public static void main(String[] args) {
  53.  
  54.         String input[] = {"ab", "ab", "aa", "zd", "ac"};
  55.  
  56.         String[] arr = util(input);
  57.  
  58.         List<Pair> count = new ArrayList<>(126);
  59.  
  60.         for (int i = 0; i < 126; i++) {
  61.  
  62.             count.add(new Pair());
  63.  
  64.         }
  65.  
  66.         for (int j = 1; j <= arr[0].length(); j++) {
  67.  
  68.             for (String arr1 : arr) {
  69.                 Pair pair = count.get(arr1.charAt(arr[0].length() - j));
  70.                 pair.count++;
  71.                 pair.words.add(arr1);
  72.             }
  73.  
  74.  
  75.             int i = 0;
  76.  
  77.             for (int k = 96; k < count.size(); k++) {
  78.                 Pair pair = count.get(k);
  79.                 if (pair.count != 0) {
  80.                     for (String word : pair.words) {
  81.                         arr[i++] = word;
  82.                     }
  83.                 }
  84.             }
  85.  
  86.  
  87.             count.forEach((pair) -> {
  88.                 pair.words.clear();
  89.             });
  90.  
  91.         }
  92.  
  93.         for (int i = 0; i < arr.length; i++) {
  94.             StringBuilder r = new StringBuilder(arr[i]);
  95.            
  96.             for (int j = 0; j < r.length(); j++) {
  97.                
  98.                 if (r.charAt(j) == '}') {
  99.                     r = r.deleteCharAt(j);
  100.                     j--;
  101.                 }
  102.             }
  103.  
  104.             System.out.print(r + " ");
  105.         }
  106.  
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement