Given a word, how do I get the list of all words, that differ by one letter?

Posted by user187809 on Stack Overflow See other posts from Stack Overflow or by user187809
Published on 2011-01-02T15:22:12Z Indexed on 2011/01/02 15:53 UTC
Read the original article Hit count: 237

Filed under:
|
|

Let's say I have the word "CAT". These words differ from "CAT" by one letter (not the full list)

  • CUT
  • CAP
  • PAT
  • FAT
  • COT
  • etc.

Is there an elegant way to generate this? Obviously, one way to do it is through brute force.

pseduo code:

while (0 to length of word)
    while (A to Z)
        replace one letter at a time, and check if the resulting word is a valid word

If I had a 10 letter word, the loop would run 26 * 10 = 260 times.

Is there a better, elegant way to do this?

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about words