createDAG method
Implementation
Map<int, List<int>> createDAG(String sentence) {
Map<int, List<int>> dag = {};
DictSegment trie = wordDict!.getTrie();
List<String> chars = sentence.charArray;
int N = chars.length;
int i = 0, j = 0;
while (i < N) {
Hit hit = trie.match(chars, i, j - i + 1);
if (hit.isPrefix() || hit.isMatch()) {
if (hit.isMatch()) {
if (!dag.containsKey(i)) {
List<int> value = [];
dag[i] = value;
value.add(j);
} else {
dag[i]?.add(j);
}
}
j += 1;
if (j >= N) {
i += 1;
j = i;
}
} else {
i += 1;
j = i;
}
}
for (i = 0; i < N; ++i) {
if (!dag.containsKey(i)) {
List<int> value = [];
value.add(i);
dag.put(i, value);
}
}
return dag;
}