← Accenture bank

Accenture · Question 74 · Dynamic Programming

Regular Expression Matching

Dynamic ProgrammingC++code⏱ O(m×n)💾 O(m×n)
Service-based

Problem

Implement regex matching with support for '.' and '*'.  '.' matches any single character.  '*' matches zero or more of the preceding element.

Approach

Use dynamic programming to compare substrings and patterns.

Time O(m×n)Space O(m×n)

How it works

  1. 1Empty pattern matches empty string (plus a* cases).
  2. 2'*' skips its char or consumes one matching char.

Sample input / output

Input:
s = "aa", p = "a*"
Output:
true

Constraints

1 <= s, p lengths <= 20.

Solution — read it, then walk it

Time O(m×n)Space O(m×n)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>include <string>include <vector>using namespace stdbool isMatch(std::string s,std::string p)DEFINE main()cout << boolalpha <<isMatch("aa", "a*") << endlRETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/9

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits