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

The Archive Base Latest Questions

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

Implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single

  • 0

Implement regular expression matching with support for ‘.’ and ‘*’.

‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the entire input string (not partial).

Some examples:

isMatch(“aa”,”a”) → false

isMatch(“aa”,”aa”) → true

isMatch(“aaa”,”aa”) → false

isMatch(“aa”, “a*”) → true

isMatch(“aa”, “.*”) → true

isMatch(“ab”, “.*”) → true

isMatch(“aab”, “c*a*b”) → true

The author gives the following solution, which is really beautiful.

bool isMatch(const char *s, const char *p) {
  assert(s && p);
  if (*p == '\0') return *s == '\0';

  // next char is not '*': must match current character
  if (*(p+1) != '*') {
    assert(*p != '*');
    return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
  }
  // next char is '*'
  while ((*p == *s) || (*p == '.' && *s != '\0')) {
    if (isMatch(s, p+2)) return true;
    s++;
  }
  return isMatch(s, p+2);
}

The author also gives some further thoughts:

If you think carefully, you can exploit some cases that the above code runs in exponential complexity.

Could you think of some examples? How would you make the above code
more efficient?

I came up one case that takes a long time to get the result while the
length of string s and p are not huge.

s[] = “aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”
p[] =”a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*b”

Can anyone help me verify this answer?
How to think this kind of finding extreme testing questions?

  • 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-11T13:15:57+00:00Added an answer on June 11, 2026 at 1:15 pm

    The best why to understand why your case exhibits exponential behavior is to first experiment with the code a bit and then try to glean from it some empirical data and make hypotheses.

    First, let’s add some simple “logging”:

    #include <cassert> 
    #include <cstdio>
    using namespace std;
    
    int count = 0;
    
    bool isMatch(const char *s, const char *p) {
        printf("%5d   %s   %s\n", count++, s, p);  
        .
        .
        .
    

    Now let’s run a number of experiments, making sure to reset the count before each experiment (remember in real code global variables are to be avoided 🙂 )

    isMatch("a", "a*b");
    isMatch("aa", "a*a*b");
    isMatch("aaa", "a*a*a*b");
    isMatch("aaaa", "a*a*a*a*b");
    isMatch("aaaaa", "a*a*a*a*a*b");
    isMatch("aaaaaa", "a*a*a*a*a*a*b");
    

    You can look at the outputs of each, and look at the number of lines generated for each and ask yourself “how does the number of recursive calls grow as I lengthen my string?” (Classic empirical algorithm analysis!)

    I’ve done the aaa case for you here: http://ideone.com/8t2kS

    You can see it took 34 steps. Look at the output; it should give you some insight into the nature of the matching. And do try for strings of increasing length. Happy experimenting.

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

Sidebar

Related Questions

There are three different solutions to implement regular expression matching: DFA, NFA and Backtracking.
I need to implement a regular expression in my asp.net mvc(C#) application using jquery.
I am currently attempting to implement a regular expression engine. (Yes, for fun. Go
I would like to know how to implement a regular expression into the following
I'd like to implement a RegExp (regular expression) that can check a string to
Regular expressions allows for the pattern matching syntax shown below. I'm trying to implement
I am trying to implement a regular expression to allow only one or two
In response to Python regular expression I tried to implement an HTML parser using
I am attempting to configure a regular expression that matches anything numerical value but
I have a regular expression I need to implement. The rules are 2 digits

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.