alestane icon

Dictionary tree (hard confirm)

alestane | PRO | 02/03/14 03:34:15 PM UTC | 0 ⭐ | 347 👁️ | Never ⏰ | []
Lua |

500 B

|

None

|

0 👍

/

0 👎

local dictionary = {}
 
local function insertWord(dict, word)
    local head, tail = word:upper():match('^(%a)(%a*)$')
    if not head then return false; end
    if not dict[head] then dict[head] = {}; end
    if tail == '' then
        dict[head][''] = true
        return true
    else
        return insertWord(dict[head], tail)
    end
end
 
insertWord(dictionary, 'BAG')
 
if dictionary['B'] and dictionary['B']['A'] and dictionary['B']['A']['G'] and dictionary['B']['A']['G'][''] then
    print(true)
else
    print(false)
end

Comments