Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7bb4e6

Browse files
feat(js): add modular solution and documentation for hard challenge 8 - Regular Expression Matching with DP
1 parent 2be0410 commit e7bb4e6

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Regular Expression Matching with Dynamic Programming
2+
3+
## English
4+
5+
Regular expression matching is a fundamental problem in computer science, with applications in text processing, compilers, and search engines. Dynamic programming provides an efficient way to handle complex patterns with branching and repetition.
6+
7+
This challenge involves designing a regular expression matching engine that supports special characters like '.' and '*', using dynamic programming techniques.
8+
9+
### Relevant Code Snippet
10+
11+
```javascript
12+
class RegexMatcher {
13+
constructor(text, pattern) {
14+
this.text = text;
15+
this.pattern = pattern;
16+
this.dp = Array.from({ length: text.length + 1 }, () =>
17+
new Array(pattern.length + 1).fill(false)
18+
);
19+
}
20+
21+
isMatch() {
22+
this.dp[0][0] = true;
23+
24+
for (let j = 2; j <= this.pattern.length; j++) {
25+
if (this.pattern[j - 1] === '*') {
26+
this.dp[0][j] = this.dp[0][j - 2];
27+
}
28+
}
29+
30+
const matches = (i, j) => {
31+
if (
32+
this.pattern[j - 1] === '.' ||
33+
this.pattern[j - 1] === this.text[i - 1]
34+
) {
35+
return this.dp[i - 1][j - 1];
36+
}
37+
if (this.pattern[j - 1] === '*') {
38+
const zero = this.dp[i][j - 2];
39+
const oneOrMore =
40+
(this.pattern[j - 2] === '.' ||
41+
this.pattern[j - 2] === this.text[i - 1]) &&
42+
this.dp[i - 1][j];
43+
return zero || oneOrMore;
44+
}
45+
return false;
46+
};
47+
48+
for (let i = 1; i <= this.text.length; i++) {
49+
for (let j = 1; j <= this.pattern.length; j++) {
50+
this.dp[i][j] = matches(i, j);
51+
}
52+
}
53+
54+
return this.dp[this.text.length][this.pattern.length];
55+
}
56+
}
57+
```
58+
59+
### History
60+
61+
Regular expression matching has been extensively studied in computer science and is widely used in text processing, compilers, and search engines.
62+
63+
---
64+
65+
## Español
66+
67+
Coincidencia de Expresiones Regulares con Programación Dinámica
68+
69+
La coincidencia de expresiones regulares es un problema fundamental en ciencias de la computación, con aplicaciones en procesamiento de texto, compiladores y motores de búsqueda. La programación dinámica proporciona una forma eficiente de manejar patrones complejos con branching y repetición.
70+
71+
Este reto consiste en diseñar un motor de coincidencia de expresiones regulares que soporte caracteres especiales como '.' y '*', usando técnicas de programación dinámica.
72+
73+
### Fragmento de Código Relevante
74+
75+
```javascript
76+
class RegexMatcher {
77+
constructor(text, pattern) {
78+
this.text = text;
79+
this.pattern = pattern;
80+
this.dp = Array.from({ length: text.length + 1 }, () =>
81+
new Array(pattern.length + 1).fill(false)
82+
);
83+
}
84+
85+
isMatch() {
86+
this.dp[0][0] = true;
87+
88+
for (let j = 2; j <= this.pattern.length; j++) {
89+
if (this.pattern[j - 1] === '*') {
90+
this.dp[0][j] = this.dp[0][j - 2];
91+
}
92+
}
93+
94+
const matches = (i, j) => {
95+
if (
96+
this.pattern[j - 1] === '.' ||
97+
this.pattern[j - 1] === this.text[i - 1]
98+
) {
99+
return this.dp[i - 1][j - 1];
100+
}
101+
if (this.pattern[j - 1] === '*') {
102+
const zero = this.dp[i][j - 2];
103+
const oneOrMore =
104+
(this.pattern[j - 2] === '.' ||
105+
this.pattern[j - 2] === this.text[i - 1]) &&
106+
this.dp[i - 1][j];
107+
return zero || oneOrMore;
108+
}
109+
return false;
110+
};
111+
112+
for (let i = 1; i <= this.text.length; i++) {
113+
for (let j = 1; j <= this.pattern.length; j++) {
114+
this.dp[i][j] = matches(i, j);
115+
}
116+
}
117+
118+
return this.dp[this.text.length][this.pattern.length];
119+
}
120+
}
121+
```
122+
123+
### Historia
124+
125+
La coincidencia de expresiones regulares ha sido ampliamente estudiada en ciencias de la computación y es ampliamente utilizada en procesamiento de texto, compiladores y motores de búsqueda.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RegexMatcher.js - Regular Expression Matching using Dynamic Programming in JavaScript
2+
3+
class RegexMatcher {
4+
constructor(text, pattern) {
5+
this.text = text;
6+
this.pattern = pattern;
7+
this.dp = Array.from({ length: text.length + 1 }, () =>
8+
new Array(pattern.length + 1).fill(false)
9+
);
10+
}
11+
12+
isMatch() {
13+
this.dp[0][0] = true;
14+
15+
for (let j = 2; j <= this.pattern.length; j++) {
16+
if (this.pattern[j - 1] === "*") {
17+
this.dp[0][j] = this.dp[0][j - 2];
18+
}
19+
}
20+
21+
const matches = (i, j) => {
22+
if (
23+
this.pattern[j - 1] === "." ||
24+
this.pattern[j - 1] === this.text[i - 1]
25+
) {
26+
return this.dp[i - 1][j - 1];
27+
}
28+
if (this.pattern[j - 1] === "*") {
29+
const zero = this.dp[i][j - 2];
30+
const oneOrMore =
31+
(this.pattern[j - 2] === "." ||
32+
this.pattern[j - 2] === this.text[i - 1]) &&
33+
this.dp[i - 1][j];
34+
return zero || oneOrMore;
35+
}
36+
return false;
37+
};
38+
39+
for (let i = 1; i <= this.text.length; i++) {
40+
for (let j = 1; j <= this.pattern.length; j++) {
41+
this.dp[i][j] = matches(i, j);
42+
}
43+
}
44+
45+
return this.dp[this.text.length][this.pattern.length];
46+
}
47+
}
48+
49+
export default RegexMatcher;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Challenge:
3+
Design a regular expression matching engine that supports special characters like '.' and '*', using dynamic programming techniques to efficiently handle branching and backtracking.
4+
5+
This solution follows DRY principles and is implemented in JavaScript.
6+
*/
7+
8+
import RegexMatcher from "./RegexMatcher.js";
9+
10+
function main() {
11+
const text = "aab";
12+
const pattern = "c*a*b";
13+
14+
const matcher = new RegexMatcher(text, pattern);
15+
const result = matcher.isMatch();
16+
17+
console.log(
18+
`Does the text "${text}" match the pattern "${pattern}"?`,
19+
result
20+
);
21+
}
22+
23+
main();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /