-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathsuffixTree.js
43 lines (34 loc) · 899 Bytes
/
suffixTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Based off of work by Eike Send (https://github.com/eikes/suffixtree)
* Based off of work found here: https://github.com/eikes/suffixtree/blob/master/js/suffixtree.js
*/
const Node = require('./suffixTreeNode');
class SuffixTree {
constructor(startingString) {
this.reset();
if (startingString) {
this.add(startingString);
}
}
add(stringToAdd) {
for (let i = 0; i < stringToAdd.length; i++) {
this.head.add(stringToAdd.slice(i));
}
this.words.push(stringToAdd);
}
getLongestSubstring() {
return this.head.getLongestSubstring();
}
reset() {
this.head = new Node();
this.words = [];
}
getLongestPalindrome() {
this.words.forEach((word) => {
const reversedWord = word.split('').reverse().join('');
this.add(reversedWord);
});
return this.getLongestSubstring();
}
}
module.exports = SuffixTree;