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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:34:40+00:00 2026-05-25T22:34:40+00:00

Problem: write a program in any language which, given a string of characters, generates

  • 0

Problem: write a program in any language which, given a string of characters, generates a regex that matches any anagram of the input string. For all regexes greater than some length N, The regex must be shorter than the “brute force” solution listing all possible anagrams separated by “|”, and the length of the regex should grow “slowly” as the input string grows (ideally linearly, but possibly n ln n).

Can you do it? I’ve tried, but my attempts are so far from succeeding, that I’m beginning to doubt it’s possible. The only reason I ask is I thought I had seen a solution on another site, but much pointless googling failed to uncover it a second time.

  • 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-25T22:34:42+00:00Added an answer on May 25, 2026 at 10:34 pm

    I think this javascript code will work according to your specifications. The regex length will increase linearly with the length of the input. It generates a regex which uses positive lookahead to match the anagram of the input string. The lookahead part of regex makes sure all the characters are present in the test input string ignoring their order and the matching part ensures that the length of the test input string is same as the length of the input string (for which regex is constructed).

    function anagramRegexGenerator(input) {
        var lookaheadPart = '';
        var matchingPart = '^';
        var positiveLookaheadPrefix='(?=';
        var positiveLookaheadSuffix=')';
        var inputCharacterFrequencyMap = {}
        for ( var i = 0; i< input.length; i++ )
        {
            if (!inputCharacterFrequencyMap[input[i]]) {
                inputCharacterFrequencyMap[input[i]] = 1
            } else {
                ++inputCharacterFrequencyMap[input[i]];
            }
        }
        for ( var j in inputCharacterFrequencyMap) {
            lookaheadPart += positiveLookaheadPrefix;
            for (var k = 0; k< inputCharacterFrequencyMap[j]; k++) {
                lookaheadPart += '.*';
                if (j == ' ') {
                    lookaheadPart += '\\s';
                } else {
                    lookaheadPart += j;
                }
                matchingPart += '.';
            }
            lookaheadPart += positiveLookaheadSuffix;
        }
        matchingPart += '$';
        return lookaheadPart + matchingPart;
    }

    Sample input and output is the following

    anagramRegexGenerator('aaadaaccc')
    //generates the following string.
    "(?=.*a.*a.*a.*a.*a)(?=.*d)(?=.*c.*c.*c)^.........$"
    anagramRegexGenerator('abcdef ghij'); 
    //generates the following string.
    "(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*\s)(?=.*g)(?=.*h)(?=.*i)(?
    =.*j)^...........$" 
    //test run returns true
    /(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)(?=.*\s)(?=.*g)(?=.*h)(?=.*i)(?
    =.*j)^...........$/.test('acdbefghij ')
    //or using the RegExp object
    //this returns true
    new RegExp(anagramRegexGenerator('abcdef ghij')).test('acdbefghij ') 
    //this returns false
    new RegExp(anagramRegexGenerator('abcdef ghij')).test('acdbefghijj') 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I write a C program, I encountered a problem that is as follows:
Problem statement: It is necessary for me to write a code, whether which before
My understanding is that it means that one can potentially write a program to
I need to write a program implementing the visitor design pattern. The problem is
I want to write a program to gives grammar as input and change it
Recently for a programming class, we were given the assignment to write a program
I am trying to write a simple program that lets me overlay a dot
I need to write an accounting routine for a program I am building that
I've been trying to write a program in java that can download a number
I'm trying to write a program which executes make.exe from MinGW distribution in the

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.