Created
July 2, 2017 22:50
-
-
Save jianminchen/22d2a9893d61e92f0c74c981ad0dc4a1 to your computer and use it in GitHub Desktop.
Leetcode 10 regular expression match - study code, figure out how to avoid using memo to solve time out issue.
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace StringMatchingTailRecursive | |
{ | |
class Program | |
{ | |
/// <summary> | |
/// source code link: | |
/// https://discuss.leetcode.com/topic/38077/my-concise-20-line-c-solution-using-recursion | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) | |
{ | |
} | |
/// <summary> | |
/// code review on July 2, 2-17 | |
/// 3:41 pm | |
/// </summary> | |
/// <param name="s"></param> | |
/// <param name="p"></param> | |
/// <param name="sIndex"></param> | |
/// <param name="pIndex"></param> | |
/// <returns></returns> | |
bool IsMatch(string s, string p, int sIndex, int pIndex) | |
{ | |
//base case - reached end of pattern | |
if (pIndex >= p.Length) | |
{ | |
return sIndex >= s.Length && pIndex >= p.Length; | |
} | |
if (pIndex + 1 < p.Length && p[pIndex + 1] == '*') | |
{ //peek ahead for * | |
while (sIndex < s.Length && (s[sIndex] == p[pIndex] || p[pIndex] == '.')) | |
{ | |
// * pattern - 0 times used, just skip .* or any char followed by * | |
if (IsMatch(s, p, sIndex, pIndex + 2)) | |
{ | |
return true; | |
} | |
// one time, instead of using recursive, using iterative. | |
// Skip the first char | |
sIndex++; | |
} | |
return IsMatch(s, p, sIndex, pIndex + 2); | |
} | |
else if (sIndex < s.Length && (s[sIndex] == p[pIndex] || p[pIndex] == '.')) | |
{ //direct 1-to-1 match | |
return IsMatch(s, p, sIndex + 1, pIndex + 1); | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment