Puzzle Solution: Search all words in Advanced Word in PHP

So, I have a database of words from 3 to 20 characters long. I want to code something in PHP, which finds all the smaller words that are contained in a big word. For example, the word “inside” contains the words “rain,” “victory,” “deliverance,” etc.

At first I thought about adding a field to the word tables (Words3 through Words20, indicating the number of letters in the words), something like "LetterCount" ... for example, a rally will be represented as 10000000000200000100000010: 1 instance of the letter A, 0 copies of the letter B , ... 2 copies of the letter L, etc. Then go through all the words in each table (or one table if the indicated length of the found words is indicated) and compare the LetterCount of each word with the LetterCount of the original word (“inside” in the example above).

But then I started to think that this puts too much strain on the MySQL database, as well as on the PHP script, invoking each Word LetterCount, comparing each digit with the original word, etc.

Is there an easier, perhaps more intuitive way to do this? I am open to using stored procedures if this helps with overhead. Only some suggestions would be greatly appreciated. Thank!

+2
source share
1 answer

Here is a simple solution that should be quite effective, but will only work up to a certain word size (maybe about 15-20 characters it will break, depending on whether the letters that make up the word are low-frequency letters with lower meanings or high-frequency letters with higher values):

  • . , e 2, t= 3, a= 5 .., .
  • , bigint. , tea 3*2*5=30. , , teat 3*2*5*3=90.
  • , , rain, , inward, , rain inward. inward = 14213045, rain = 7315 14213045 7315, rain inward.
  • bigint 9223372036854775807, 15-20 ( ). , 20- , anitinstitutionalism, 6901041299724096525, bigint. 14- xylopyrography 635285791503081662905, . , , , , , , , .

, : http://www.sqlfiddle.com/#!2/9bd27/8

+6

All Articles