|
| 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. |
0 commit comments