Skip to main content

WildCard Matching algorithm

The logic is quite simple. We do the following 3 basic checks before we do the comparison:

  1. Check for nulls and return if either of the String or Pattern is null

  2. Check if we have reached the end of string. If so, return true

  3. If either of string or pattern are left over, return false

  4. One case that we need to explicitly handle is when string is EMPTY and pattern is made solely of one or more *s.


  1. If it is a char or ?, skip 1 ahead in both string and pattern

  2. If it is a *, return the result of

    • string+1,pattern+1 - For handling multiple *s

    • string,pattern+1     - For handling null *

    • string+1,pattern     - For handling multi-char *

The actual code is pretty simple. The following code has a complexity of around 2^n for an sized string.

[code language="cpp"]
bool OnlyAskterisk(char* Pattern)
if (*Pattern == '\0') return true;
if (*Pattern != '*') return false;
else return OnlyAskterisk(Pattern + 1);

bool isMatch(char *String, char *Pattern)
//Base case checks
if (*String=='\0' &&OnlyAskterisk(Pattern)) return true;
if (!String || !Pattern) return false;
if (*String == '\0' && *Pattern == '\0') return true;
if (*String == '\0' || *Pattern == '\0') return false; //Tricky case which is used to eliminate stack overflows with * variable

//Actual comparisons
if ((*String == *Pattern) || (*Pattern == '?'))
return isMatch(String + 1, Pattern + 1);
//Multimatch- StringMoves, No Match-PatternMoves SingleMatch- BothMove. Single match is to handle last char being *
if (*Pattern == '*') return (isMatch(String + 1, Pattern) || isMatch(String + 1, Pattern + 1) || isMatch(String, Pattern + 1));
return false;



  1. What would happen if the input is an empty string and the pattern is a*

  2. Thanks for pointing the mistake out. I've updated the code to handle the edge. Do let me know in case you find any more issues.

  3. What would happen if the input is string = "*AB" and pattern = "*?"


Post a Comment

Popular posts from this blog


GraphQL What is GraphQL It is a specification laid out by Facebook which proposed an alternative way to query and modify data. Think of it is an as a complimentary of REST/RPC. Now head here and read the  original graphQL documentation . It will take 2-3 hours tops but is a worthy read. This will help you build some impressions on it and help contrast against mine below: Why use GraphQL Core advantage Instead of defining custom backend rpc/rest endpoints for every data-shape, graphql allows you to build a more general endpoint which give frontend/mobile engineers freedom and query and play with the data. It might be less efficient, add a bit more complexity (need for things like  data-loader ), harder to standardize and control client-contracts for. What it looses in complexity and control, it gains in flexibility and freedom - provided your data model is worth of a graphql-ish query  How to tell if my data-model graphql-ish? Are there complex relationships between y

About me

Hi, I'm currently working as a software developer in the logistics industry in San Francisco.  My aim is to impact billions of people's live for the better and make the world a better place. Cheers, Vignesh

Backend - Tech refresher 2019

Hello there As a software engineer, it is important to keep updating your skillsets by learning the latest programming-elements (includes  paradigms,  patterns,  languages,  tools and  frameworks ). This becomes a bit easy if you already working on the cutting edge of something. Even then, it is possible to go too deep and loose breadth. I've taken upon myself to do a tech refresher every year. The intent is to read, experiment and understand these elements by spending anywhere between 4 days to 4 weeks. The ultimate goal is: "do I know most  that I need to know to build a planet-scale backend tech-stack ground up" I'll write up my learnings in posts to help myself (and maybe others) refer it. Here is the initial list I'm thinking about: Redis MySQL, GraphQL Aurora, Mesos, Kubernetes Cadence, SWS Cassandra, MangoDB, NoSQL, MySQL, Spanner, S<, DynDB ELK Flink, Storm, Samza, Spark Hadoop HDFS, Yarn, MapReduce Hive, HBase Kafka, Zookeeper NW: