有人能帮助我理解下面的黄道码吗?
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?