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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:38:15+00:00 2026-06-11T02:38:15+00:00

I have a set of elements of size about 100-200. Let a sample element

  • 0

I have a set of elements of size about 100-200. Let a sample element be X.

Each of the elements is a set of strings (number of strings in such a set is between 1 and 4). X = {s1, s2, s3}

For a given input string (about 100 characters), say P, I want to test whether any of the X is present in the string.

X is present in P iff for all s belong to X, s is a substring of P.

The set of elements is available for pre-processing.


I want this to be as fast as possible within Java. Possible approaches which do not fit my requirements:

  • Checking whether all the strings s are substring of P seems like a costly operation
  • Because s can be any substring of P (not necessarily a word), I cannot use a hash of words
  • I cannot directly use regex as s1, s2, s3 can be present in any order and all of the strings need to be present as substring

Right now my approach is to construct a huge regex out of each X with all possible permutations of the order of strings. Because number of elements in X <= 4, this is still feasible. It would be great if somebody can point me to a better (faster/more elegant) approach for the same.

Please note that the set of elements is available for pre-processing and I want the solution in java.

  • 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-11T02:38:16+00:00Added an answer on June 11, 2026 at 2:38 am

    You can use regex directly:

    Pattern regex = Pattern.compile(
        "^               # Anchor search to start of string\n" +
        "(?=.*s1)        # Check if string contains s1\n" +
        "(?=.*s2)        # Check if string contains s2\n" +
        "(?=.*s3)        # Check if string contains s3", 
        Pattern.DOTALL | Pattern.COMMENTS);
    Matcher regexMatcher = regex.matcher(subjectString);
    foundMatch = regexMatcher.find();
    

    foundMatch is true if all three substrings are present in the string.

    Note that you might need to escape your “needle strings” if they could contain regex metacharacters.

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

Sidebar

Related Questions

I have a set of selected elements in jQuery: <div class=element></div> <div class=element></div> <div
I have set of 6 elements and I would like to filter by every
I have a set of unique elements (there are not two identical elements). And
I have a set of vertically ordered elements. They are displayed with the following
I have a vector of booleans. I need to set its elements from n-th
I have a simple website with a master-page. To set properties to elements on
I have a handful of elements I want to set roll overs on, how
I have set a background on the data-role=page element like so <div data-role=page style=background:
I have set the data to div element and append it to another div
I have a simple fabric.canvas element which has a background and I have set

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.