SHARE
TWEET

Untitled

a guest Jan 24th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
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
Top