Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Pair {
- public byte count;
- public List<String> words;
- public Pair() {
- words = new LinkedList<>();
- count = 0;
- }
- }
- public class CountAndRadix {
- static int max = 0;
- static int findLongestString(String... arr) {
- int maxLength = 0;
- for (String s : arr) {
- if (s.length() > maxLength) {
- maxLength = s.length();
- }
- }
- return maxLength;
- }
- static String[] util(String... arr) {
- max = findLongestString(arr);
- String res[] = new String[arr.length];
- int i = 0;
- for (String string : arr) {
- while (string.length() < max) {
- string = string.concat("}");
- }
- res[i++] = string;
- }
- return res;
- }
- public static void main(String[] args) {
- String input[] = {"ab", "ab", "aa", "zd", "ac"};
- String[] arr = util(input);
- List<Pair> count = new ArrayList<>(126);
- for (int i = 0; i < 126; i++) {
- count.add(new Pair());
- }
- for (int j = 1; j <= arr[0].length(); j++) {
- for (String arr1 : arr) {
- Pair pair = count.get(arr1.charAt(arr[0].length() - j));
- pair.count++;
- pair.words.add(arr1);
- }
- int i = 0;
- for (int k = 96; k < count.size(); k++) {
- Pair pair = count.get(k);
- if (pair.count != 0) {
- for (String word : pair.words) {
- arr[i++] = word;
- }
- }
- }
- count.forEach((pair) -> {
- pair.words.clear();
- });
- }
- for (int i = 0; i < arr.length; i++) {
- StringBuilder r = new StringBuilder(arr[i]);
- for (int j = 0; j < r.length(); j++) {
- if (r.charAt(j) == '}') {
- r = r.deleteCharAt(j);
- j--;
- }
- }
- System.out.print(r + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement