From a23a77c389b918640a30113a6a565207b3cc333a Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 4 Nov 2018 22:49:59 -0500 Subject: [PATCH 1/2] added regex --- Main.js | 4 +- Regex.js | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 Regex.js diff --git a/Main.js b/Main.js index b9011fd..c42ffe9 100644 --- a/Main.js +++ b/Main.js @@ -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(); \ No newline at end of file +// subsets.main(); +regex.main(); diff --git a/Regex.js b/Regex.js new file mode 100644 index 0000000..3717b04 --- /dev/null +++ b/Regex.js @@ -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 +*/ From 3d181cd3a5b6a4f74b0d55a69b7cfbb379a0b540 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Wed, 14 Nov 2018 11:23:25 -0500 Subject: [PATCH 2/2] Update Regex.js --- Regex.js | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/Regex.js b/Regex.js index 3717b04..e01db4c 100644 --- a/Regex.js +++ b/Regex.js @@ -98,43 +98,5 @@ var main = function(){ 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 -*/