Could someone help me understand the following pheudocode?
countWords(vertex, word, missingLetters)
k=firstCharacter(word)
if isEmpty(word)
return vertex.words
else if notExists(edges[k]) and missingLetters=0
return 0
else if notExists(edges[k])
cutLeftmostCharacter(word)
return countWords(vertex, word, missingLetters-1)
//Here we cut a character but we don't go lower in the tree
else
//We are adding the two possibilities: the first
//character has been deleted plus the first character is present
r=countWords(vertex, word, missingLetters-1)
cutLeftmostCharacter(word)
r=r+countWords(edges[k], word, missingLetters)
return r
The idea is that using a Trie we are trying to find the number of times a word appears in our dictionary, BUT we may have missing letters.
I am lost in the else part. I don’t understand the logic.
For example if the first char of our word is a match we hit the last else and then recurse on countWords in the same level but with missingLetters-1 but then isn’t that an identical loop? I.e. it will compare again the first letter in the same level and so on?
Could someone please help me figure this out?
Even if the order of the last lines was inverted as suggested by antti.huima, something still doesn’t look alright to me.
If I understood it correctly, if you have
Pizza,Lizzashould also count if missingLetters==1, right? But then, if Lizza is not in the trie you enterand next you enter
which returns 0?
Given that you already have a trie, I would suggest you take a look at the Levehnstein Distance.