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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:26:41+00:00 2026-06-11T18:26:41+00:00

Given a string S and a list L of patterns [L 1 , …,

  • 0

Given a string S and a list L of patterns [L1, …, Ln], how would you find the list of all tokens in S matching a pattern in L and so that the total number of matched letters in S is maximized?

A dummy example would be S = “thenuke”, L = {“the”, “then”, “nuke”} and we would like to retrieve [“the”, “nuke”] as if we start by matching “then”, we do not get the solution maximizing the total number of letters in S being matched.

I have been looking at other SO questions, string matching algorithms but found nothing to efficiently solve the maximization part of the problem.
This must have been studied e.g. in bioinformatics but I’m not in the field so any help (including link to academic papers) deeply appreciated!

  • 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-06-11T18:26:42+00:00Added an answer on June 11, 2026 at 6:26 pm

    This can be solved in O(|S| + |L| + k) time, where k is the total number of matches of all strings from L in S. There are two major steps:

    1. Run Aho-Corasick. This will give you all matches of any string from L in S. This runs in the same time as mentioned above.

    2. Initialize an array, A, of integers of length |S| + 1 to all zeros. March through the array, at position i set A[i] to A[i-1] if it is larger, then for every match, M, from L in S at position i, set A[i+|M|] to the max of A[i+|M|] and A[i] + |M|.

    Here is some code, in Go, that does exactly this. It uses a package I wrote that has a convenient wrapper for calling Aho-Corasick.

    package main
    
    import (
      "fmt"
      "github.com/runningwild/stringz"
    )
    
    func main() {
      input := []byte("thenuke")
      patterns := [][]byte{[]byte("hen"), []byte("thenu"), []byte("uke")}
    
      // This runs Aho-Corasick on the input string and patterns, it returns a
      // map, matches, such that matches[i] is a list of indices into input where
      // patterns[i] matches the input string.  The running time of this is
      // O(|input| + |patterns| + k) and uses O(|patterns| + k) auxillary storage,
      // where k is the total number of matches found.
      find := stringz.FindSet(patterns)
      matches := find.In(input)
    
      // We want to invert the map so that it maps from index to all strings that
      // match at that index.
      at_pos := make([][]int, len(input)+1)
      for index, hits := range matches {
        for _, hit := range hits {
          at_pos[hit] = append(at_pos[hit], index)
        }
      }
    
      // Now we do a single pass through the string, at every position we will
      // keep track of how many characters in the input string we can match up to
      // that point.
      max := make([]int, len(input)+1)
      for i := range max {
        // If this position isn't as good as the previous position, then we'll use
        // the previous position.  It just means that there is a character that we
        // were unable to match anything to.
        if i > 0 && max[i-1] > max[i] {
          max[i] = max[i-1]
        }
    
        // Look through any string that matches at this position, if its length is
        // L, then L positions later in the string we can have matched L more
        // character if we use this string now.  This might mean that we don't use
        // another string that earlier we thought we'd be matching right now, we'll
        // find out later which one was better.
        for _, hit := range at_pos[i] {
          L := len(patterns[hit])
          if i+L < len(max) && max[i+L] < max[i]+L {
            max[i+L] = max[i] + L
          }
        }
      }
    
      fmt.Printf("%v\n", max)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
Need some help to find the number of matched tokens between two strings. I
Given this code: List<string> things = new List<string>(); foreach (string thing in things) {
I've been looking for a way to hash a given string in C# that
Algorithm to generate all possible letter combinations of given string down to 2 letters
Given this bit of Makefile: # for pattern matching $(OBJDIR) := build # just
I've got a list of exact patterns that I want to search in a
I'm using Directory.GetFiles() to list files according to given pattern. This works fine for
I am trying to use string matching in a winforms application .I would like
I have a list of strings. Each string follows the pattern {Path}\UpdateTo{Version}-{Order}. I need

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.