Skip to content

added regex #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added regex
  • Loading branch information
ignacio-chiazzo committed Nov 5, 2018
commit a23a77c389b918640a30113a6a565207b3cc333a
4 changes: 3 additions & 1 deletion Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js");
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
var subsets = require("./Subsets.js");
var regex = require("./Regex.js");

// Invocation

// permutationWithoutDuplicates.main();
// permutationWithDuplicates.main();
// subsets.main();
// subsets.main();
regex.main();
140 changes: 140 additions & 0 deletions Regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
URL: https://leetcode.com/problems/regular-expression-matching/description/

Regular Expression Matching
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).

Note:

s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

* @param {*} s
* @param {*} p
*/

var isMatch = function(s, p) {
return isMatchAux(s, p, 0, 0);
};

var isMatchAux = function(str, pattern, posStr, posPat) {
if(posStr == str.length)
return posPat == pattern.length || canBeZero(pattern, posPat);

if(posPat < pattern.length - 1 && pattern.charAt(posPat + 1) == "*") {
const valuePattern = pattern.charAt(posPat);
posPat = posPat + 2;

if (isMatchAux(str, pattern, posStr, posPat)) { // 0 matches
return true
}

while(posStr < str.length && (str.charAt(posStr) === valuePattern || valuePattern === ".")) {
if(isMatchAux(str, pattern, posStr + 1, posPat)) {
return true;
}
posStr++;
}
} else if(str.charAt(posStr) === pattern.charAt(posPat) || pattern.charAt(posPat) === ".") {
return isMatchAux(str, pattern, posStr + 1, posPat + 1);
}

return false;
}

var canBeZero = function(pattern, posPat) {
while(posPat < pattern.length && pattern.charAt(posPat) == "*" ||
posPat < pattern.length - 1 && pattern.charAt(posPat + 1) == "*") {
posPat++;
}

return posPat == pattern.length;
}

var main = function(){
console.log(isMatch("aa", "a"));
console.log(isMatch("aa", "a*"));
console.log(isMatch("a","ab*"));
console.log(isMatch("ab", ".*"));
console.log(isMatch("aab", "c*a*b"));
console.log(isMatch("mississippi", "mis*is*p*."));
}



main();
module.exports.main = main;


/*
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false
*/