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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:09:37+00:00 2026-06-17T09:09:37+00:00

Given a set strings of containing numbers, how can I find those strings that

  • 0

Given a set strings of containing numbers, how can I find those strings that are the superset. For example if strings ‘139 24’ and ‘139 277 24’ appear then I want to keep ‘139 277 24’ as ‘139 24’ can be found inside it. Also these numbers may appear in any order inside a string.

               '24'
              '277'
           '277 24'
           '139 24'
       '139 277 24'
          '139 277'
              '139'
           '136 24'
       '136 277 24'
          '136 277'
              '136'
       '136 139 24'
   '136 139 277 24'
      '136 139 277'
          '136 139'
              '246'

The result for the above data is given below.

   '136 139 277 24'
              '246'

Edit: I am splitting each string and putting individual numbers in a set and then comparing this through the sets created from the whole list. I can find a solution using this approach but I think there should be some other elegant way to perform the same.

I was trying the following code and felt that it is becoming unnecessarily complex.

#First create a set of tuples
allSeqsTuple = set()
for seq in allSeqs: #allSeqs store the sequences described above
    x = seq.split()
    allSeqsTuple.add(tuple(x))


#For each 'allSeqs', find if all the items in that seq that is in 'allSeqsTuple'. 
for line in allSeqs:
    x = set(line.split())
    result = findContainment(x, allSeqsTuple)
    ......
    ......

def findContainment(x, allSeqsTuple):
    contained = False
    for y in allSeqsTuple:
        cntnd = bool(x-set(y))
        if (cntnd):
            contained = True
            continue
        else:
            break
    return contained
  • 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-17T09:09:37+00:00Added an answer on June 17, 2026 at 9:09 am

    Let’s make a laundry list of the major players in this problem:

    • strings, e.g. '24 139 277'
    • sets — a collection of “superstrings”
    • superset inclusion — the <= set operator
    • splitting the strings into a set of number-strings: e.g. set(['24', '139', '277'])

    We are given a list of strings, but what we’d really like — what would be more useful — is a list of sets:

    In [20]: strings = [frozenset(s.split()) for s in strings]    
    In [21]: strings
    Out[21]: 
    [frozenset(['24']),
     frozenset(['277']),
     ...
     frozenset(['136', '139']),
     frozenset(['246'])]
    

    The reason for frozensets will become apparent shortly. I’ll explain why, below. The reason why we want sets at all is because that have a convenient superset comparison operator:

    In [22]: frozenset(['136']) <= frozenset(['136', '139', '24'])
    Out[22]: True
    
    In [23]: frozenset(['136']) <= frozenset(['24', '277'])
    Out[23]: False
    

    This is exactly what we need to determine if one string is a superstring of another.

    So, basically, we want to:

    • Start with an empty set of superstrings = set()
    • Iterate through strings: for s in strings.
    • As we examine each s in strings, we will add new ones to
      superstrings if they are not a subset of a item already in
      superstrings.
    • For each s, iterate through a set of superstrings: for sup in superstrings.

      • Check if s <= sup — that is, if s is a subset of sup, quit the loop since s is smaller than some known superstring.

      • Check if sup <= s — that is, if
        s a superset of some item in superstrings. In this case, remove the item in superstrings and replace it with s.

    Technical notes:

    • Because we are removing items from superstrings, we can not also
      iterate over superstrings itself. So, instead, iterate over a copy:

      for sup in superstrings.copy():
      
    • And finally, we would like superstrings to be a set of sets. But
      the items in a set have to be hashable, and sets themselves are not
      hashable. But frozensets are, so it is possible to have a set of
      frozensets. This is why we converted strings into a list of
      frozensets.

    strings = [
        '24', '277', '277 24', '139 24', '139 277 24', '139 277', '139', '136 24',
        '136 277 24', '136 277', '136', '136 139 24', '136 139 277 24', '136 139 277',
        '136 139', '246']
    
    def find_supersets(strings):
        superstrings = set()
        set_to_string = dict(zip([frozenset(s.split()) for s in strings], strings))
        for s in set_to_string.keys():
            for sup in superstrings.copy():
                if s <= sup:
                    # print('{s!r} <= {sup!r}'.format(s = s, sup = sup))
                    break
                elif sup < s:
                    # print('{sup!r} <= {s!r}'.format(s = s, sup = sup))
                    superstrings.remove(sup)
            else:
                superstrings.add(s)
        return [set_to_string[sup] for sup in superstrings]
    
    print(find_supersets(strings))
    

    yields

    ['136 139 277 24', '246']
    

    It turns out this is faster than pre-sorting the strings:

    def using_sorted(strings):
        stsets = sorted(
            (frozenset(s.split()) for s in strings), key=len, reverse=True)
        superstrings = set()
        for stset in stsets:
            if not any(stset.issubset(s) for s in superstrings):
                superstrings.add(stset)
        return superstrings
    
    In [29]: timeit find_supersets(strings)
    100000 loops, best of 3: 18.3 us per loop
    In [25]: timeit using_sorted(strings)
    10000 loops, best of 3: 24.9 us per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
Given a set of questions that have linked survey and category id: > db.questions.find().toArray();
Given a set of real numbers drawn from a unknown continuous univariate distribution (let's
Given a set of letters, say from A.. F, how can one generate a
I am trying to validate that the given string contains contains only letters, numbers,
Given a non-negative integer n and an arbitrary set of inequalities that are user-defined
I'm trying to set up a parser which, given a value, can assign it
Given a string containing a mathematical expression, given a set of functions/commands and given
I have some phone numbers (validated such that all containing only integers, no -
Given a string s , what is the fastest method to generate a 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.