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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:31:20+00:00 2026-06-13T22:31:20+00:00

Is there an efficient Regex to assert that two string share the same pattern

  • 0

Is there an efficient Regex to assert that two string share the same pattern of repeated characters.

("tree", "loaa") => true
("matter", "essare") => false
("paper", "mime") => false
("acquaintance", "mlswmodqmdlp") => true
("tree", "aoaa") => false

Event if it’s not through Regex, I’m looking for the most efficient way to perform the task

  • 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-13T22:31:21+00:00Added an answer on June 13, 2026 at 10:31 pm

    The easiest way is probably to walk through both strings manually at the same time and build up a dictionary (that matces corresponding characters) while you are doing it:

    if(input1.Length != input2.Length)
        return false;
    var characterMap = new Dictionary<char, char>();
    for(int i = 0; i < input1.Length; i++)
    {
        char char1 = input1[i];
        char char2 = input2[i];
        if(!characterMap.ContainsKey(char1))
        {
            if (characterMap.ContainsValue(char2))
                return false;
            characterMap[char1] = char2;
        }
        else
        {
            if(char2 != characterMap[char1])
                return false;
        }
    }
    return true;
    

    In the same manner you could construct a regex. This is certainly not more efficient for a single comparison, but it might be useful if you want to check one repetition pattern against multiple strings in the future. This time we associate characters with their back-references.

    var characterMap = new Dictionary<char, int>();
    string regex = "^";
    int nextBackreference = 1;
    for(int i = 0; i < input.Length; i++)
    {
        char character = input[i];
        if(!characterMap.ContainsKey(character))
        {
            regex += "(.)";
            characterMap[character] = nextBackreference;
            nextBackreference++;
        }
        else
        {
            regex += (@"\" + characterMap[character]);
        }
    }
    regex += "$";
    

    For matter it will generate this regex: ^(.)(.)(.)\3(.)(.)$. For acquaintance this one: ^(.)(.)(.)(.)\1(.)(.)(.)\1\6\2(.)$. If could of course optimize this regular expression a bit afterwards (e.g. for the second one ^(.)(.)..\1.(.).\1\3\2$), but in any case, this would give you a reusable regex that checks against this one specific repetition pattern.

    EDIT: Note that the given regex solution has a caveat. It allows mapping of multiple characters in the input string onto a single character in the test strings (which would contradict your last example). To get a correct regex solution, you would have to go a step further to disallow characters already matched. So acquaintance would have to generate this awful regular expression:

    ^(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3)(.)\1(?!\1|\2|\3|\4)(.)(?!\1|\2|\3|\4|\5)(.)(?!\1|\2|\3|\4|\5|\6)(.)\1\6\2(?!\1|\2|\3|\4|\5|\6|\7)(.)$
    

    And I cannot think of an easier way, since you cannot use backreferences in (negated) character classes. So maybe, if you do want to assert this as well, regular expressions are not the best option in the end.

    Disclaimer: I am not really a .NET guru, so this might not be the best practice in walking through arrays in building up a dictionary or string. But I hope you can use it as a starting point.

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

Sidebar

Related Questions

What is the most efficient regex that will match these domains, without having to
Is there an efficient workflow to mirror a project that is mainly hosted on
Let's say I have these two strings: 5/15/1983 and 1983.05.15. Assume that all characters
Is there a convenient way to write a regex that will try to match
Need a little help with a regex I'm trying to match a string that
Is there an efficient implementation of matlab's deconv in python? # Convolve z=conv(x, y)
Are there any efficient bitwise operations I can do to get the number of
Is there an efficient way to version store procedures written in PL/SQL? (I only
Is there an efficient way to clone an object yet leave out specified properties?
I want to schedule events to happen for my users. Is there an efficient

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.