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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:36:35+00:00 2026-05-22T11:36:35+00:00

You are given a string and an array of strings. How to quickly check,

  • 0

You are given a string and an array of strings. How to quickly check, if this string can be built by concatenating some of the strings in the array?

This is a theoretical question, I don’t need it for practical reasons. But I would like to know, if there is some good algorithm for this.

EDIT
Reading some answer I have noticed, that this is probably NP-Complete problem. Even finding a subset of strings, that will together have same length, as a given string is a classic subset sum problem.

So I guess there is no easy answer to this.

EDIT

Now it seems, that it is not a NP-Complete problem after all. That’s way cooler 🙂

EDIT

I have came up with a solution that passes some tests:

def can_build_from_substrings(string, substrings):
    prefixes = [True] + [False] * (len(string) - 1)
    while True:
        old = list(prefixes)
        for s in substrings:
            for index, is_set in enumerate(prefixes):
                if is_set and string[index:].startswith(s):
                    if string[index:] == s:
                        return True
                    prefixes[index + len(s)] = True
        if old == prefixes: # nothing has changed in this iteration
            return False

I believe the time is O(n * m^3), where n is length of substrings and m is length of string. What do you think?

  • 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-22T11:36:36+00:00Added an answer on May 22, 2026 at 11:36 am

    Note: I assume here that you can use each substring more than once. You can generalize the solution to include this restriction by changing how we define subproblems. That will have a negative impact on space as well as expected runtime, but the problem remains polynomial.

    This is a dynamic programming problem. (And a great question!)

    Let’s define composable(S, W) to be true if the string S can be written using the list of substrings W.

    S is composable if and only if:

    1. S starts with a substring w in W.
    2. The remainder of S after w is also composable.

    Let’s write some pseudocode:

    COMPOSABLE(S, W):
      return TRUE if S = "" # Base case
      return memo[S] if memo[S]
    
      memo[S] = false
    
      for w in W:
        length <- LENGTH(w)
        start  <- S[1..length]
        rest   <- S[length+1..-1]
        if start = w AND COMPOSABLE(rest, W) :
          memo[S] = true # Memoize
    
      return memo[S]
    

    This algorithm has O(m*n) runtime, assuming the length of the substrings is not linear w/r/t to the string itself, in which case runtime would be O(m*n^2) (where m is the size of the substring list and n is the length of the string in question). It uses O(n) space for memoization.

    (N.B. as written the pseudocode uses O(n^2) space, but hashing the memoization keys would alleviate this.)

    EDIT

    Here is a working Ruby implementation:

    def composable(str, words)
      composable_aux(str, words, {})
    end
    
    def composable_aux(str, words, memo)
      return true if str == ""                # The base case
      return memo[str] unless memo[str].nil?  # Return the answer if we already know it
    
      memo[str] = false              # Assume the answer is `false`
    
      words.each do |word|           # For each word in the list:
        length = word.length
        start  = str[0..length-1]
        rest   = str[length..-1]
    
        # If the test string starts with this word,
        # and the remaining part of the test string
        # is also composable, the answer is true.
        if start == word and composable_aux(rest, words, memo)
          memo[str] = true           # Mark the answer as true
        end
      end
    
      memo[str]                      # Return the answer
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string. I need to replace all instances of a given array
Say I have an array of Strings declared like this: String[] strings = new
I want to check whether a string is built from another two strings within
Given a string s and an array of smaller strings, T, design a method
Given an array of n Objects, let's say it is an array of strings
I would like to sort an array of strings in a directory, given a
How do I create an array in smarty from a given string like 22||33||50
I have string that I want to chop to array of substrings of given
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
How can I drop all tables whose names begin with a given string? I

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.