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 6629143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:13:06+00:00 2026-05-25T22:13:06+00:00

I would like to use backtracking to search for all substrings in a long

  • 0

I would like to use backtracking to search for all substrings in a long string allowing for variable length matches – that is matches allowing for a maximum given number of mismatches, insertions, and deletions. I have not been able to locate any useful examples. The closest I have found is this paper here, but that is terribly complex. Anyone?

Cheers,

Martin

  • 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-25T22:13:06+00:00Added an answer on May 25, 2026 at 10:13 pm

    Algorithm

    The function ff() below uses recursion (i.e. backtracking) to solve your problem. The basic idea is that at the start of any call to f(), we are trying to match a suffix t of the original “needle” string to a suffix s of the “haystack” string, while allowing only a certain number of each type of edit operation.

    // ss is the start of the haystack, used only for reporting the match endpoints.
    void f(char* ss, char* s, char* t, int mm, int ins, int del) {
        while (*s && *s == *t) ++s, ++t;    // OK to always match longest segment
        if (!*t) printf("%d\n", s - ss);    // Matched; print endpoint of match
        if (mm && *s && *t) f(ss, s + 1, t + 1, mm - 1, ins, del);
        if (ins && *s) f(ss, s + 1, t, mm, ins - 1, del);
        if (del && *t) f(ss, s, t + 1, mm, ins, del - 1);
    }
    
    // Find all occurrences of t starting at any position in s, with at most
    // mm mismatches, ins insertions and del deletions.
    void ff(char* s, char* t, int mm, int ins, int del) {
        for (char* ss = s; *s; ++s) {
    //      printf("Starting from offset %d...\n", s - ss);
            f(ss, s, t, mm, ins, del);
        }
    }
    

    Example call:

    ff("xxabcydef", "abcdefg", 1, 1, 1);
    

    This outputs:

    9
    9
    

    because there are two ways to find “abcdefg” in “xxabcydef” with at most 1 of each kind of change, and both of these ways end at position 9:

    Haystack: xxabcydef-
    Needle:     abc-defg
    

    which has 1 insertion (of y) and 1 deletion (of g), and

    Haystack: xxabcyde-f
    Needle:     abc-defg
    

    which has 1 insertion (of y), 1 deletion (of f), and 1 substitution of g to f.

    Dominance Relation

    It may not be obvious why it’s actually safe to use the while loop on line 3 to greedily match as many characters as possible at the start of the two strings. In fact this may reduce the number of times that a particular end position will be reported as a match, but it will never cause an end position to be forgotten completely — and since we’re usually interested in just whether or not there is a match ending at a given position of the haystack, and without this while loop the algorithm would always take time exponential in the needle size, this seems a win-win.

    It is guaranteed to work because of a dominance relation. To see this, suppose the opposite — that it is in fact unsafe (i.e. misses some matches). Then there would be some match in which an initial segment of equal characters from both strings are not aligned to each other, for example:

    Haystack: abbbbc
    Needle:   a-b-bc
    

    However, any such match can be transformed into another match having the same number of operations of each type, and ending at the same position, by shunting the leftmost character following a gap to the left of the gap:

    Haystack: abbbbc
    Needle:   ab--bc
    

    If you do this repeatedly until it’s no longer possible to shunt characters without requiring a substitution, you will get a match in which the largest initial segment of equal characters from both strings are aligned to each other:

    Haystack: abbbbc
    Needle:   abb--c
    

    My algorithm will find all such matches, so it follows that no match position will be overlooked by it.

    Exponential Time

    Like any backtracking program, this function will exhibit exponential slowdowns on certain inputs. Of course, it may be that on the inputs you happen to use, this doesn’t occur, and it works out faster than particular implementations of DP algorithms.

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

Sidebar

Related Questions

First of all sorry for my English. I would like to use a backtracking
I would like to use a language that I am familiar with - Java,
I would like to use a component that exposes the datasource property, but instead
I would like use argparse to parse the arguments that it knows and then
I currently have some large strings that I would like use as test data
Designing my RESTful API, I would like use following URI http://[HOST]/[PLANET]/[LAT];[LONG] e.g. http://myserver/earth/50.2;29.1 What
I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use client-side Javascript to perform a DNS lookup (hostname to

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.