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

  • Home
  • SEARCH
  • 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 3432572
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:27:57+00:00 2026-05-18T07:27:57+00:00

I am looking for an algorithm, preferably in Python that would help me locate

  • 0

I am looking for an algorithm, preferably in Python that would help me locate substrings, N characters long, of exisiting strings that are closest to a target string N character long.

Consider the target string, that is, say, 4 characters long, to be:

targetString -> '1111'

Assume this is the string I have available with me ( I will generate substrings of this for “best alignment” matching ):

nonEmptySubStrings -> ['110101']

Substrings of the above that are 4 characters long:

nGramsSubStrings -> ['0101', '1010', '1101']

I want to write/use a “Magic Function” that would select the string closest to targetString :

someMagicFunction -> ['1101']

Some more examples:

nonEmptySubStrings -> ['101011']
nGramsSubStrings -> ['0101', '1010', '1011']

someMagicFunction -> ['1011']

nonEmptySubStrings -> ['10101']
nGramsSubStrings -> ['0101', '1010']

someMagicFunction -> ['0101', '1010']

Is this “Magic Function” a well known substring problem?

I really want to find the min. number of changes in nonEmptySubStrings so that it would have targetString as a substring.

  • 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-18T07:27:58+00:00Added an answer on May 18, 2026 at 7:27 am

    Base on OP’s comment to question, this is what is desired

    import functools
    
    def edit_distance(str1, str2): 
        #implement it here
    
    f = functools.operator(edit_distance, target_string)
    return min(f(s) for s in slices(string_))   # use slices from below
    

    This will return the minimum edit distance of any substring to the target string. It will not indicate which string that is or what its index is. It could be easily modified to do
    so though.


    The naive way, which can be the best way, is

    import functools
    
    def diff(str1, str2):
        # However you test the distance gets defined here. e.g. Hamming distance, 
        # Levenshtein distance, etc.
    
    
    def slices(string_, L):
        for i in xrange(len(string_) - L + 1)):
            yield string_[i:i+L]
    
    best_match = min(slices(string_), key=functools.partial(diff, target_string))
    

    This wont return the index at which the substring occurs though. Of course you didn’t specify that you need it in your question 😉

    If you want to get better than this, it will depend on how you’re measuring the distance and will basically boil down to avoiding checking some substrings by infering that you would have to change at least x chars to get a better match than you already have. At that point, you might as well just change x chars by jumping ahead x chars.

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

Sidebar

Related Questions

I'm looking for an algorithm that sorts strings similar to the way files (and
I am looking for a simple unpatented one-way encryption algorithm, preferably in c. I
I'm looking for an algorithm that rotates an image by some degrees (input). public
I'm looking for an algorithm (preferably in C/C++/Java or similar) to sort a multiset.
I am looking for an algorithm that will allow me to visually separate any
i am looking for an algorithm ( in pseudo code) that generates the 3d
Im looking for an algorithm to be used in a racing game Im making.
I'm looking for an algorithm to detect if two rectangles intersect (one at an
I am looking for an algorithm to calculate the next set of operations in
I'm looking for an algorithm to generate permutations of a set in such a

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.