|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +
|
| 5 | +-* 472. Concatenated Words *- |
| 6 | +
|
| 7 | +Given an array of strings words (without duplicates), return all the concatenated words in the given list of words. |
| 8 | +
|
| 9 | +A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array. |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] |
| 16 | +Output: ["catsdogcats","dogcatsdog","ratcatdogcat"] |
| 17 | +Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; |
| 18 | +"dogcatsdog" can be concatenated by "dog", "cats" and "dog"; |
| 19 | +"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat". |
| 20 | +Example 2: |
| 21 | +
|
| 22 | +Input: words = ["cat","dog","catdog"] |
| 23 | +Output: ["catdog"] |
| 24 | + |
| 25 | +
|
| 26 | +Constraints: |
| 27 | +
|
| 28 | +1 <= words.length <= 104 |
| 29 | +1 <= words[i].length <= 30 |
| 30 | +words[i] consists of only lowercase English letters. |
| 31 | +All the strings of words are unique. |
| 32 | +1 <= sum(words[i].length) <= 105 |
| 33 | +
|
| 34 | +
|
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | +*/ |
| 39 | + |
| 40 | +import 'dart:collection'; |
| 41 | + |
| 42 | +class A { |
| 43 | + bool checkConcatenate(String word, Set<String> s) { |
| 44 | + for (int i = 1; i < word.length; i++) { |
| 45 | + String prefixWord = word.substring(0, i); |
| 46 | + String suffixWord = word.substring(i, word.length); |
| 47 | + if (s.contains(prefixWord) && |
| 48 | + (s.contains(suffixWord) || checkConcatenate(suffixWord, s))) |
| 49 | + return true; |
| 50 | + } |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + List<String> findAllConcatenatedWordsInADict(List<String> words) { |
| 55 | + HashSet<String> s = HashSet(); |
| 56 | + List<String> concatenateWords = []; |
| 57 | + for (String word in words) s.add(word); |
| 58 | + for (String word in words) { |
| 59 | + if (checkConcatenate(word, s) == true) concatenateWords.add(word); |
| 60 | + } |
| 61 | + return concatenateWords; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class B { |
| 66 | + List<String> findAllConcatenatedWordsInADict(List<String> words) { |
| 67 | + HashSet<String> wordsSet = HashSet(); |
| 68 | + for (String word in words) wordsSet.add(word); |
| 69 | + List<String> res = []; |
| 70 | + |
| 71 | + for (String word in words) { |
| 72 | + int n = word.length; |
| 73 | + List<int> dp = List.filled(n + 1, 0); |
| 74 | + dp[0] = 1; |
| 75 | + for (int i = 0; i < n; i++) { |
| 76 | + if (dp[i] != 0) continue; |
| 77 | + for (int j = i + 1; j <= n; j++) { |
| 78 | + if (j - i < n && wordsSet.contains(word.substring(i, j - i))) { |
| 79 | + dp[j] = 1; |
| 80 | + } |
| 81 | + } |
| 82 | + if (dp[n] == 0) { |
| 83 | + res.add(word); |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return res; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class C { |
| 93 | + List<String> findAllConcatenatedWordsInADict(List<String> words) { |
| 94 | + List<String> result = []; |
| 95 | + HashSet<String> set = HashSet(); |
| 96 | + for (String word in words) set.add(word); |
| 97 | + for (String word in words) helper(result, set, word); |
| 98 | + return result; |
| 99 | + } |
| 100 | + |
| 101 | + void helper(List<String> list, HashSet<String> set, String word) { |
| 102 | + if (word.length == 0) return; |
| 103 | + List<bool> dp = List.filled(word.length + 1, false); |
| 104 | + dp[0] = true; |
| 105 | + for (int i = 0; i < word.length; i++) { |
| 106 | + if (!dp[i]) continue; |
| 107 | + for (int j = i + 1; j < dp.length; j++) { |
| 108 | + if (i == 0 && j == word.length) continue; |
| 109 | + if (set.contains(word.substring(i, j))) dp[j] = true; |
| 110 | + } |
| 111 | + } |
| 112 | + if (dp[dp.length - 1]) list.add(word); |
| 113 | + } |
| 114 | +} |
0 commit comments