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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:52:31+00:00 2026-06-07T17:52:31+00:00

I have two strings, string1 and string2. I want to check if string1 can

  • 0

I have two strings, string1 and string2. I want to check if string1 can be made up of characters in string2 (without repeating characters”). For instance, if string1 is “tool” and string2 is “atoll”, the function will return false. If string1 is “touch” and string2 is “chetoudce”, it will return true.

What is the most efficient way to do this in Javascript? I was thinking of using indexOf and then removing the characters that are used from string2 to build string1, but I think creating this auxiliary string might have performance issues.

edit: I made this based on the first response, here it is:

function isSubsetOf(a, b){
    if(a.length > b.length){
        return false;
    }

    while(a.length > 0){
        var letter = a.substr(0, 1),
            re = new RegExp(a.substr(0, 1), 'g'),
            a_count = (a.match(re)||[]).length,
            b_count = (b.match(re)||[]).length;

        if(a_count > b_count){
            return false;
        }

        a = a.replace(re, '');
    }
    return true;
}
  • 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-07T17:52:32+00:00Added an answer on June 7, 2026 at 5:52 pm

    First, count the characters in each string. Then, if the superstring has greater than or equal numbers of each character than the substring, return true.

    O(m+n), for m and n are the sizes of the substring and superstring.

    Example:

    Superstring: aaaaabbbbccc
    Substring: aabbcc
    
    Superstring letters: 
        a: 5
        b: 4
        c: 3
        all others: 0
    Substring letters:
        a: 2
        b: 2
        c: 2
        all others: 0
    
    5 >= 2, 4 >= 2, 3 >= 2, so true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Perl string variables containing date values. I want to check whether
i have two strings in a php i want to make following checks in
I have two strings in a java program, which I want to mix in
I have two different arrays, one with strings and another with ints. I want
I have two MySQL tables, and I want to find and replace text strings
I have two strings str1 and str2 . Check both of them and see
I want to check whether a string is built from another two strings within
I have to compare two strings such as INTU and IXTE and check if
I have two sets of strings ( A and B ), and I want
I have two possible widths for a string i want to display in a

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.