Find all words inside a word

The major weakness of your algorithm is that, for every subword, you need to compare it to every other word in the dictionary. You don’t need to do that, really — if your word begins with ‘a’, you don’t really need to see if it matches words that begin with ‘b’. If the next letter is ‘c’, then you don’t really care to compare it to words that begin with ‘d’. The question then becomes: «How do I implement this idea efficiently?»

To do this, we can create a tree to represent all of the words in the dictionary. We construct this tree by taking each word in the dictionary and extending the tree with it, and shading in the last node.

Example Tree

When we want to test if a subword is in this tree, we just go through that word letter by letter and use those letters to determine where to go next in the tree (starting at the top). If we find that we have nowhere to go, or that we land on a non-shaded tree node after going through the whole subword, then it’s not a word. Otherwise, it is a word if we land on a shaded node. The effect of this is that we can search the entire dictionary at once, rather than one word at a time. The cost of this is, of course, a bit of set-up at the start, but that’s not a great price to pay if you have a lot of words in the dictionary.

Well that’s all pretty fantastic! Let’s try implementing it:

class Node:
    def __init__( self, parent, valid_subword ):
        self.parent = parent
        self.valid_subword = valid_subword
        self.children = {}

    #Extend the tree with a new node
    def extend( self, transition, makes_valid_word ):
        next_node = None
        if transition in self.children:
            if makes_valid_word:
                self.children[transition].makes_valid_word = True
        else:
            self.children[transition] = Node( self, makes_valid_word )
        return self.children[transition]

def generateTree( allwords ):
  tree = Node( None, False )
    for word in allwords:
      makes_valid_word = False
      current_node = tree
      for i in range(len(word)):
        current_node = current_node.extend( word[i], True if i == len(word) - 1 else False )
  return tree

def checkDict( word, tree ):
    current_node = tree
    for letter in word:
        try:
            current_node = current_node.children[letter]
        except KeyError:
            return False

    return current_node.valid_subword

Then, later-on:

for word in allWords:
  for subword in subWords(word):
    checkDict(subword)
    #Code to keep track of the number of words found, like you already have

This algorithm allows you to check whether or not a word is in your dictionary in O(m) time, where m is the length of the longest word in the dictionary. Notice that this remains roughly constant for a dictionary containing an arbitrary number of words. Your original algorithm was O(n) per check, where n is the number of words in the dictionary.

dCode

Search for a tool

Words Containing …

Tool to search words that contain a given letter or sequence of letters, pattern search or suit of letters, containing special letters but not some others, etc.

Results

Words Containing …

Tag(s) : Word Search

Share

Share

dCode and more

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!

Words Containing …

  1. Games and Solvers
  2. Word Search
  3. Words Containing …

List words that contain some letters

More criteria

Dedicated tools

Answers to Questions (FAQ)

How to find words containing some letters?

dCode allows to search and find words with some letters in them, by patterns of letters in a word (including jokers or ?), or by specific letters in any position (at the beginning, in the middle or at the end).

Example: Search for words containing the letters ABC in any order gives: CAB, JACOB etc.

Example: Search for words containing the pattern A-A-A-A gives: ALABAMA, etc.

Example: Search for words including the exact pattern ABC gives: LABCOAT, etc. (Letters are necessarily consecutive)

Example: Search for words having the letters ABC in this order: FABRIC, PAYBACK etc.

To find word with a maximum of letters, use the longest word solver.

To find a word with letters shuffled in any order, use the anagram solver.

To find words with letters in certain positions, use the crossword solver.

It is also possible to exclude some letters (words containing some letters but not some others).

How to cheat at letters games?

All letter games (crosswords, scrabble, boggle, wordle, etc.) already have dedicated pages on dCode but some variants can be solved with this tool.

Example: Find a word with the following letters: ORWD. The generator will find WORD (that is an anagram)

Source code

dCode retains ownership of the «Words Containing …» source code. Except explicit open source licence (indicated Creative Commons / free), the «Words Containing …» algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, translator), or the «Words Containing …» functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and all data download, script, or API access for «Words Containing …» are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!
Reminder : dCode is free to use.

Cite dCode

The copy-paste of the page «Words Containing …» or any of its results, is allowed as long as you cite dCode!
Exporting results as a .csv or .txt file is free by clicking on the export icon
Cite as source (bibliography):
Words Containing … on dCode.fr [online website], retrieved on 2023-04-14, https://www.dcode.fr/words-containing

French (Français) Spanish (Español)

Summary

  • List words that contain some letters
  • More criteria
  • Dedicated tools
  • How to find words containing some letters?
  • How to cheat at letters games?

Similar pages

Support

  • Paypal
  • Patreon
  • More

Forum/Help

Discuss

Keywords

word,containing,contain,find,with,middle,letter,some,pattern,content,having,search

Links

  • Contact
  • About dCode
  • dCode App
  • Wikipedia

https://www.dcode.fr/words-containing

© 2023 dCode — The ultimate ‘toolkit’ to solve every games / riddles / geocaching / CTF.

 

Fast and sharp word finder for fun and education

Crossword Mode

Finds words containing given letters («w??d» — «word», «wood»).

Enter a pattern. Use a question mark (?) or a dot (.) for unknown letters.

Tap here for Xworder Mobile. xworder.com/m

Xworder provides word search tools designed to help you solve and compose crosswords
and other word puzzles, learn new words and have fun!

Xworder features:

Find words if you know some of the letters that it contains («w??d» — «word», «wood»).

Find words that can be built from the given set of letters («scrabble» — «laser»,
«barbel»).

Find words and word combinations by rearranging all letters from the given set («anagram»
— «a rag man»).

A fun game of building word chains by changing one letter at a time («break — bread
— tread — trend»).

Switching between the Full and Limited word lists makes it easier to find what you
are looking for.

xworder logo

© 2009 — 2011 Xworder.
x-mark
How to use Xworder

Scrabble® is a registered trademark of Hasbro, Inc. in the USA and Canada.

Outside of the USA and Canada, the Scrabble® trademark is owned by Mattel, Inc.

Like this post? Please share to your friends:
  • Find hidden words from a word
  • Find all words in microsoft word
  • Find different types of fiction in the word write them next to different
  • Find function in word is not working
  • Find all words in excel