My idea is as follows
I used Trie Tree to store the names. Then I used DFS to find out the total length.
Suppose I am given following names
Code: Select all
aac
aad
ace
Code: Select all
[:3]

[a:3]
/ \
[a:2] [c:1]
/ \ \
[c:1] [d:1] [e:1]
Here, every node follows this format [character : number of names it occurs].
So total length will be = 3 + 3 + 2 = 8
Is my approach wrong or am I making any silly mistake here?