Created
December 17, 2017 19:24
-
-
Save jianminchen/712d71b3e40500760df21403ea9ce8e1 to your computer and use it in GitHub Desktop.
regular expression - matching issue - with bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Solution | |
{ | |
// Empty Text | |
// T = "" | |
// - P "" => T | |
// - P "a*" => T | |
public static bool IsMatch(string text, string pattern) | |
{ | |
// Don't think we need 12-15 | |
if (pattern == null || pattern.Length == 0) // Case: Pattern is empty | |
{ // If text is empty => True, otherwise =>false | |
return text == null || text.Length == 0; | |
} | |
int lengthText = text.Length; // run time exception | |
int lengthPattern = pattern.Length; | |
var memo = new int[lengthText + 1, lengthPattern + 1]; // 0 - not set, 1 - true, -1 - false | |
return IsMatchHelper(text, pattern, memo, 0, 0 ); | |
} | |
/// recursive | |
private static bool IsMatchHelper(string text, string pattern, int[,] memo, int textIndex, int patternIndex) //abbb, ab* | |
{ | |
int lengthText = text.Length; // run time exception // 4 | |
int lengthPattern = pattern.Length; // 3 | |
// base case | |
if(patternIndex == lengthPattern) // false | |
{ | |
// if both pattern and text are empty | |
// then it's a match otherwise we failed to match | |
memo[textIndex, lengthText] = text.Length == lengthText ? 1 : -1; | |
return memo[textIndex, lengthText] == 1; // false | |
} | |
// "" "a" | |
// ^ | |
// | |
//"aaaa", "aaaab" | |
// "", a*, a*b*, a*.* | |
if(textIndex == lengthText) // "" | |
{ | |
return (patternIndex + 1 < lengthPattern && pattern[patternIndex + 1] != '*') && IsMatchHelper(text, pattern, memo, textIndex, patternIndex + 2) ; | |
} | |
if(memo[textIndex, patternIndex] != 0) | |
{ | |
return memo[textIndex, patternIndex] == 1; // | |
} | |
// we have to do the work | |
if(patternIndex + 1 < lengthPattern && pattern[patternIndex + 1] != '*') // a*, .* | |
{ | |
// do one char comparison first | |
var visit = text[textIndex]; | |
var patternChar = pattern[patternIndex]; | |
var isSameChar = isSame(visit, patternChar ); | |
if(isSameChar) | |
{ | |
var nextIteration = IsMatchHelper( text, pattern, memo, textIndex + 1, patternIndex + 1); | |
memo[textIndex, patternIndex] = nextIteration? 1 : - 1; | |
return nextIteration; | |
} | |
else | |
{ | |
memo[textIndex, patternIndex] = -1; // | |
return false; | |
} | |
} | |
else | |
{ | |
// a* case , repeat one time first, repeat zero time | |
// repeat one time | |
// a a*, .*, first char should be the same | |
var visit = text[textIndex]; | |
var patternChar = pattern[patternIndex]; | |
var firstRecursiveMatchOneTime = isSame(visit, patternChar) && IsMatchHelper( text, pattern, memo, textIndex + 1, patternIndex); | |
if(firstRecursiveMatchOneTime) | |
{ | |
memo[textIndex, patternIndex] = 1; | |
return true; | |
} | |
var secondRecurisveMatchZeroTime = IsMatchHelper( text, pattern, memo, textIndex, patternIndex + 2); | |
if(secondRecurisveMatchZeroTime) | |
{ | |
memo[textIndex, patternIndex] = 1; | |
return true; | |
} | |
return false; | |
} | |
} | |
private static bool isSame(char visit, char patternChar ) | |
{ | |
return (visit == patternChar) || (patternChar == '.'); | |
} | |
static void Main(string[] args) | |
{ | |
} | |
} | |
// "aa", "a" -> false | |
// "aa", "aa" -> true | |
// "abc", "a.c" -> . matches any char, true | |
// "abbb", "ab*" -> | |
// b* match 0 or 1 and 1 more time | |
// two branches -> | |
// "bbb" match "" - 0 time -> false | |
// "bb" match "b*" -> match 1 time - subproblem - re | |
// time out -> apply to test case | |
// "abcdabcdabcdabcdabcdabcd", "a*b*c*d*a*b*c*d*a*b*c*d*a*b*c*d*a*b*c*d*" -> time out , match, 2^20 timeout | |
// "bb" match "b*" -> match 1 time - subproblem - re | |
// "bbb" match "" - 0 time -> false | |
// | |
// "acd" "ab*c." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment