Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Solution {
  4.     static List<String> radixSortMSD(List<String> words) {
  5.         if (words == null) {
  6.             return null;
  7.         }
  8.  
  9.         return radix(words, 0);
  10.     }
  11.  
  12.     static LinkedList<String> radix(List<String> words, int index) {
  13. LinkedList<String> res = new LinkedList<>();
  14.         @SuppressWarnings("unchecked")
  15.         LinkedList<String>[] buckets = new LinkedList[26];
  16.        
  17.         for(int i = 0; i < buckets.length; i++) {
  18.             buckets[i] = new LinkedList<String>();
  19.         }
  20.        
  21.         for(String w : words) {
  22.             if(w.length() - 1 < index) {
  23.                 res.add(w);
  24.             }
  25.         }
  26.        
  27.         for(String w : words) {
  28.             if(w.length()-1 >= index) {
  29.                 int idx = w.charAt(index) - 'a';
  30.                 buckets[idx].add(w);
  31.             }
  32.         }
  33.        
  34.         for(int i = 0; i < buckets.length; i++) {
  35.             if(buckets[i].size() > 0) {
  36.                 buckets[i] = radix(buckets[i], index+1);
  37.             }
  38.         }
  39.        
  40.         for(int i = 0; i < buckets.length; i++) {
  41.             for(String w : buckets[i]) {
  42.                 res.add(w);
  43.             }
  44.         }
  45.         return res;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement