Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit eba528a

Browse files
added regex (ignacio-chiazzo#8)
* added regex * Update Regex.js
1 parent 010d4a8 commit eba528a

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

Main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ var loadProblems = () => {
3030
});
3131
}
3232

33-
main();
33+
main();

Regex.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
URL: https://leetcode.com/problems/regular-expression-matching/description/
3+
4+
Regular Expression Matching
5+
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.
6+
7+
'.' Matches any single character.
8+
'*' Matches zero or more of the preceding element.
9+
The matching should cover the entire input string (not partial).
10+
11+
Note:
12+
13+
s could be empty and contains only lowercase letters a-z.
14+
p could be empty and contains only lowercase letters a-z, and characters like . or *.
15+
Example 1:
16+
17+
Input:
18+
s = "aa"
19+
p = "a"
20+
Output: false
21+
Explanation: "a" does not match the entire string "aa".
22+
Example 2:
23+
24+
Input:
25+
s = "aa"
26+
p = "a*"
27+
Output: true
28+
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
29+
Example 3:
30+
31+
Input:
32+
s = "ab"
33+
p = ".*"
34+
Output: true
35+
Explanation: ".*" means "zero or more (*) of any character (.)".
36+
Example 4:
37+
38+
Input:
39+
s = "aab"
40+
p = "c*a*b"
41+
Output: true
42+
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
43+
Example 5:
44+
45+
Input:
46+
s = "mississippi"
47+
p = "mis*is*p*."
48+
Output: false
49+
50+
* @param {*} s
51+
* @param {*} p
52+
*/
53+
54+
var isMatch = function(s, p) {
55+
return isMatchAux(s, p, 0, 0);
56+
};
57+
58+
var isMatchAux = function(str, pattern, posStr, posPat) {
59+
if(posStr == str.length)
60+
return posPat == pattern.length || canBeZero(pattern, posPat);
61+
62+
if(posPat < pattern.length - 1 && pattern.charAt(posPat + 1) == "*") {
63+
const valuePattern = pattern.charAt(posPat);
64+
posPat = posPat + 2;
65+
66+
if (isMatchAux(str, pattern, posStr, posPat)) { // 0 matches
67+
return true
68+
}
69+
70+
while(posStr < str.length && (str.charAt(posStr) === valuePattern || valuePattern === ".")) {
71+
if(isMatchAux(str, pattern, posStr + 1, posPat)) {
72+
return true;
73+
}
74+
posStr++;
75+
}
76+
} else if(str.charAt(posStr) === pattern.charAt(posPat) || pattern.charAt(posPat) === ".") {
77+
return isMatchAux(str, pattern, posStr + 1, posPat + 1);
78+
}
79+
80+
return false;
81+
}
82+
83+
var canBeZero = function(pattern, posPat) {
84+
while(posPat < pattern.length && pattern.charAt(posPat) == "*" ||
85+
posPat < pattern.length - 1 && pattern.charAt(posPat + 1) == "*") {
86+
posPat++;
87+
}
88+
89+
return posPat == pattern.length;
90+
}
91+
92+
var main = function(){
93+
console.log(isMatch("aa", "a"));
94+
console.log(isMatch("aa", "a*"));
95+
console.log(isMatch("a","ab*"));
96+
console.log(isMatch("ab", ".*"));
97+
console.log(isMatch("aab", "c*a*b"));
98+
console.log(isMatch("mississippi", "mis*is*p*."));
99+
}
100+
101+
main();
102+
module.exports.main = main;

0 commit comments

Comments
 (0)