0

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

I am trying to find words consist of letters inside the keyword. However, the letters shouldn't be used again once it's detected that it's inside the word.

i keep the words in the dictionary.

For example I have the keyword: "asdasdas"

My code says hi in these conditions

  consistLetters("asdasdas","asdas")
  consistLetters("asdasdas","sad")
  consistLetters("asdasdas","sss")

However it also says hi when i consume all of the "s" in the keyword which is not what i want:

  consistLetters("asdasdas","ssss")

How to stop this? Thank you.

EDIT: I solved my problem and shared it as an answer. I hope it'll be helpful.

Community
  • 1
  • 1
Figen Güngör
  • 12,169
  • 14
  • 66
  • 108
  • I would solve this by making a histogram for both inputs and then checking if for each letter that the value in the second histogram is less than or equal to the value in the first histogram, then if that is so return `true` else return `false`. – Dan D. Jun 26 '12 at 14:05
  • If you have solved your question, please post it as answer, and mark it as answered. That way it might be helpful to others. Otherwise it will remain unanswered on the site. Thanks! – jpjacobs Jun 26 '12 at 18:02

1 Answers1

0

Here is my code:

  function consistLetters(keyword,word)

keywordletters={ }
    wordletters= { }
local found=false
findLetters(keyword,keywordletters)
findLetters(word,wordletters)


for i=1, #wordletters,1 do
    for j=1, #keywordletters,1 do
        if(keywordletters[j]~="") then
            if(wordletters[i]==keywordletters[j]) then
                keywordletters[j]="" 
                found=true;
                break
            end
         end
    end
    if found~=true then
        return false
    end
      found=false;  
    end     

  end
Figen Güngör
  • 12,169
  • 14
  • 66
  • 108