Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Regular Expression Matching problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Regular Expression Matching problem solution, we have given an input string s and a pattern p, implement regular expression matching with support for ‘.’ and ‘*’ where:

  1. ‘.’ Matches any single character.
  2. ‘*’ Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Leetcode Regular Expression Matching problem solution

Leetcode Regular Expression Matching problem solution in Python.

class Solution:
    def isMatch(self, s, p):
        matches = [[False for j in range(len(s) + 1)] for i in range(len(p) + 1)]
        matches[0][0] = True
        for i in range(2, len(p) + 1):
            if p[i - 1] == "*": 
                matches[i][0] = matches[i - 2][0]
        for i in range(1, len(p) + 1):
            for j in range(1, len(s) + 1):
                if p[i -1] == s[j - 1] or p[i - 1] == ".":
                    matches[i][j] = matches[i - 1][j - 1]
                elif p[i - 1] == "*": 
                    if p[i - 2] != s[j - 1] and p[i - 2] != ".":
                        matches[i][j] = matches[i - 2][j]
                    else:
                        matches[i][j] = matches[i - 2][j] or matches[i -1][j] or matches[i][j - 1] 
        return matches[-1][-1]

Regular Expression Matching problem solution in Java.

public boolean isMatch(String s, String p) {
        if (p == null || p.length() == 0) return (s == null || s.length() == 0);
        
        boolean dp[][] = new boolean[s.length()+1][p.length()+1];
        dp[0][0] = true;
        for (int j=2; j<=p.length(); j++) {
            dp[0][j] = p.charAt(j-1) == '*' && dp[0][j-2]; 
        }
        
        for (int j=1; j<=p.length(); j++) {
            for (int i=1; i<=s.length(); i++) {
                if (p.charAt(j-1) == s.charAt(i-1) || p.charAt(j-1) == '.') 
                    dp[i][j] = dp[i-1][j-1];
                else if(p.charAt(j-1) == '*')
                    dp[i][j] = dp[i][j-2] || ((s.charAt(i-1) == p.charAt(j-2) || p.charAt(j-2) == '.') && dp[i-1][j]); 
            }
        }
        return dp[s.length()][p.length()];
    }

Problem solution in C++.

class Solution {
public:
    bool isMatch(string s, string p) {
        int m=s.length(),n=p.length();
        bool dp[m+1][n+1];
        memset(dp,false,sizeof(dp));dp[0][0]=true;
        for(int i=0;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(p[j-1]!='.' && p[j-1]!='*')
                {
                    if(i>0 && s[i-1]==p[j-1] && dp[i-1][j-1])
                        dp[i][j]=true;
                }
                else if(p[j-1]=='.')
                {
                    if(i>0 && dp[i-1][j-1])
                        dp[i][j]=true;
                }
                else if(j>1)
                {
                    if(dp[i][j-1] || dp[i][j-2])
                        dp[i][j]=true;
                    else if(i>0 && (p[j-2]==s[i-1] || p[j-2]=='.') && dp[i-1][j] )
                        dp[i][j]=true;
                }
            }
        }
        return dp[m][n];
    }
};

Problem solution in C.

bool isMatch(char* s, char* p) {
    for (;*p && *(p + 1) != '*'; ++s, ++p)
        if (!*s || (*p != *s && *p != '.'))
            return false;
        
    if (!*p)
        return *s == 0;
    
    if (*s && (*s == *p || *p == '.'))
        return isMatch(s, p + 2) || isMatch(s + 1, p);
    else
        return isMatch(s, p + 2);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes