Advertisement
1988coder

524. Longest Word in Dictionary through Deleting

Dec 29th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. /**
  2.  * Pre Processing Time : O(words.length)
  3.  *
  4.  * Time Complexity: O(S.length() + Summation of all chars in all words)
  5.  *
  6.  * Space Complexity: O(words.length)
  7.  */
  8. class Solution {
  9.     class Word {
  10.         String word;
  11.         int index;
  12.  
  13.         Word(String w) {
  14.             word = w;
  15.             index = 0;
  16.         }
  17.     }
  18.  
  19.     public String findLongestWord(String s, List<String> d) {
  20.         if (s == null || d == null || s.length() == 0 || d.size() == 0) {
  21.             return "";
  22.         }
  23.  
  24.         Map<Character, List<Word>> map = new HashMap();
  25.         for (String w : d) {
  26.             if (w.length() > s.length()) {
  27.                 continue;
  28.             }
  29.             char c = w.charAt(0);
  30.             if (!map.containsKey(c)) {
  31.                 map.put(c, new ArrayList<Word>());
  32.             }
  33.             map.get(c).add(new Word(w));
  34.         }
  35.  
  36.         String result = "";
  37.         for (char c : s.toCharArray()) {
  38.             if (!map.containsKey(c)) {
  39.                 continue;
  40.             }
  41.             List<Word> curBucket = map.get(c);
  42.             map.put(c, new ArrayList<Word>());
  43.             for (Word w : curBucket) {
  44.                 w.index++;
  45.                 if (w.word.length() == w.index) {
  46.                     if (result.length() < w.word.length()
  47.                             || (result.length() == w.word.length() && w.word.compareTo(result) < 0)) {
  48.                         result = w.word;
  49.                     }
  50.                 } else {
  51.                     char nextChar = w.word.charAt(w.index);
  52.                     if (!map.containsKey(nextChar)) {
  53.                         map.put(nextChar, new ArrayList<Word>());
  54.                     }
  55.                     map.get(nextChar).add(w);
  56.                 }
  57.             }
  58.         }
  59.         return result;
  60.     }
  61. }
  62.  
  63. /*
  64.  * Time Complexity: O(N * S)
  65.  *
  66.  * Space Complexity: O(1)
  67.  *
  68.  * N = number of words in the dictionary. S = length of string s.
  69.  */
  70. class Solution {
  71.     public String findLongestWord(String s, List<String> d) {
  72.         if (s == null || d == null || s.length() == 0 || d.size() == 0) {
  73.             return "";
  74.         }
  75.  
  76.         String result = "";
  77.         for (String word : d) {
  78.             if (word.length() > s.length() || result.length() > word.length()
  79.                     || (result.length() == word.length() && result.compareTo(word) < 0)) {
  80.                 continue;
  81.             }
  82.             int indexW = 0;
  83.             for (int indexS = 0; indexS < s.length(); indexS++) {
  84.                 if (s.charAt(indexS) == word.charAt(indexW)) {
  85.                     indexW++;
  86.                 }
  87.                 if (indexW == word.length()) {
  88.                     result = word;
  89.                     break;
  90.                 }
  91.             }
  92.         }
  93.         return result;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement