How can I find the words consist of the letters inside the keyword in Lua?

后端 未结 3 475
北恋
北恋 2021-01-24 02:30

For example, I have a keyword \"abandoned\" and I want to find the words that contains letters of this keyword such as \"done\", \"abandon\", band\", from the arrays I stored t

3条回答
  •  太阳男子
    2021-01-24 03:07

    Try this:

    W={"done", "abandon", "band"}
    for k,w in pairs(W) do
        W[w]=true
    end
    
    function findwords(s)
        for i=1,#s do
            for j=i+1,#s do
                local w=s:sub(i,j)
                if W[w] then print(w) end
            end
        end
    end
    
    findwords("abandoned")
    

    If you don't have an array of words, you can load a dictionary:

    for w in io.lines("/usr/share/dict/words") do
        W[w]=true
    end
    

提交回复
热议问题