Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3669562
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:15:31+00:00 2026-05-19T02:15:31+00:00

Let’s say I have the word CAT. These words differ from CAT by one

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:15:32+00:00Added an answer on May 19, 2026 at 2:15 am

    Given a list of words, for example with

    words = set(line.strip().lower() for line in open('/usr/share/dict/words'))
    

    you can build and index of “wildcarded” words, where you replace each character of the word with a wildcard (say “?”), so that for example “gat” and “fat” both get indexed to “?at”:

    def wildcard(s, idx):
        return s[:idx] + '?' + s[idx+1:]
    
    def wildcarded(s):
        for idx in xrange(len(s)):
            yield wildcard(s, idx)
    
    # list(wildcarded('cat')) returns ['?at', 'c?t', 'ca?']
    
    from collections import defaultdict
    index = defaultdict(list)
    
    for word in words:
        for w in wildcarded(word):
            index[w].append(word)
    

    Now if you want to look for all the words that differ by one letter from “cat”, just look for “?at”, “c?t” and “ca?” and concatenate the results:

    def near_words(word):
        ret = []
        for w in wildcarded(word):
            ret += index[w]
        return ret
    
    print near_words('cat')
    # outputs ['cat', 'bat', 'zat', 'jat', 'kat', 'rat', 'sat', 'pat', 'hat', 'oat', 'gat', 'vat', 'nat', 'fat', 'lat', 'wat', 'eat', 'yat', 'mat', 'tat', 'cat', 'cut', 'cot', 'cit', 'cay', 'car', 'cap', 'caw', 'cat', 'can', 'cam', 'cal', 'cad', 'cab', 'cag']
    print near_words('stack')
    # outputs ['stack', 'stack', 'smack', 'spack', 'slack', 'snack', 'shack', 'swack', 'stuck', 'stack', 'stick', 'stock', 'stank', 'stack', 'stark', 'stauk', 'stalk', 'stack']
    

    If the maximum word length is L and the number of words is N, the index is made of O(NL) pointers, while the lookup algorithm runs in time O(L + number of results).

    If you want to look for all the words that differ by K letters instead of 1 this approach doesn’t generalize well, but it is a very hard problem in full generality (it is the problem of finding neighbors in Hamming spaces).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a dataset, which can be neatly classified using weka's J48
Let say I have two UIViews: View1: - bounds: 0, 0, 20, 20 -
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.