Skip to content

Commit 4713a5e

Browse files
authored
Update 1698-number-of-distinct-substrings-in-a-string.js
1 parent c1aaa80 commit 4713a5e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

1698-number-of-distinct-substrings-in-a-string.js

+28
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,31 @@ const countDistinct = function(s) {
1212

1313
return set.size
1414
};
15+
16+
// another
17+
18+
/**
19+
* @param {string} s
20+
* @return {number}
21+
*/
22+
const countDistinct = function (s, count = 0) {
23+
const root = new Trie()
24+
const N = s.length
25+
for (let i = 0; i < N; i++) {
26+
let node = root
27+
for (let j = i; j < N; j++) {
28+
const c = s[j]
29+
if (!node.children.has(c)) {
30+
node.children.set(c, new Trie())
31+
count++
32+
}
33+
node = node.children.get(c)
34+
}
35+
}
36+
return count
37+
}
38+
class Trie {
39+
constructor() {
40+
this.children = new Map()
41+
}
42+
}

0 commit comments

Comments
 (0)