Open hon9g opened 4 years ago
insert
search
and prefix
in averageH = the height of the tree = max length of the inputs
/**
/**
/**
/**
/**
### Childrens in Array
- Time Complexity:
- **O(S)** for `insert`
- **O(S)** for `search` and `prefix`
- Space Complexity:
- **O(26*H)** = **O(H)**
- `H = the height of the tree = max length of the inputs`
insert
and sum
, K = length of the key
N = size of total inputs
/**
* @param {string} key
* @param {number} val
* @return {void}
*/
const Node = function(val) {
this.val = val;
this.children = new Map();
}
/**
D = length of dict * size of each word
S = length of sentence * the size of each sentence
const Node = function(x) {
this.val = x;
this.children = new Map();
}
/**
* @param {string[]} dict
* @param {string} sentence
* @return {string}
*/
var replaceWords = function(dict, sentence) {
const res = [], head = new Node('');
for (word of dict) {
let curr = head;
for (c of word) {
if (!curr.children.has(c)) {
curr.children.set(c, new Node(c));
}
curr = curr.children.get(c);
}
curr.children.set('.', new Node('.'));
}
for (word of sentence.split(' ')) {
let curr = head, newWord = [];
for (c of word) {
if (!curr.children.has(c)) break;
if (curr.children.has('.')) break;
newWord.push(c);
curr = curr.children.get(c);
}
if (newWord.length) {
res.push(curr.children.has('.') ? newWord.join('') : word);
} else {
res.push(word);
}
}
return res.join(' ');
};
complexity analysis
complexity analysis
complexity analysis
complexity analysis
Problems selected by LeetCode for the topic Trie 🌐
Click ✔️ to go to each solution. The Link to each problem🌐 is at the top of each solution.
🔗 Basic Operations
🔗 Practical Application I
🔗 Practical Application II