Created
December 26, 2017 20:58
-
-
Save jianminchen/416e5454ac0f09132821e14f86d192d4 to your computer and use it in GitHub Desktop.
Leetcode 10 - regular expression - the peer mock interview - good performance
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
// scan the text & the pattern character by character | |
// if the current char in the pattern is '.', we should just move next | |
// if the current char in the pattern is not '.' and is also not followed by '*', | |
// check if the cur char in the pattern exactly matches the cur char in the text | |
// if the current char in the pattern is followd by '*' | |
// check for different strings the the char in the pattern is repeated from 0 to the number of times it's repeated in the text starting from the current index | |
using System; | |
class Solution | |
{ | |
public static bool IsMatch(string text, string pattern, int tIndex, int pIndex) | |
{ | |
if(pIndex >= pattern.Length && tIndex >= text.Length) // correct | |
return true; | |
if(pIndex >= pattern.Lenght) //"" match "a*", "a" match "" false | |
return false; | |
if(pattern[pIndex] == '.') | |
return IsMatch(text, pattern, tIndex+1, pIndex+1); // correct | |
if(pIndex == pattern.Length-1 || pattern[pIndex+1] != '*') { // not including | |
if(text[tIndex] != pattern[pIndex]) // correct | |
return false; | |
return IsMatch(text, pattern, tIndex+1, pIndex+1); | |
} | |
// * missing * | |
// the cur char is followed by a '*' | |
// cur char in pattern is c | |
if(IsMatch(text, patter, tIndex, pIndex+2)) // char in pattern repeated 0 times | |
return true; | |
while(tIndex < text.Length && text[tIndex] == pattern[pIndex]) { | |
if(IsMatch(text, pattern, tIndex+1, pIndex+2)) // 0 times, text do not move forward, pattern move forward | |
return true; | |
tIndex++; | |
} | |
return false; | |
} | |
static void Main(string[] args) | |
{ | |
} | |
} | |
// what is your local time? 10:00pm | |
// 12 pm - www.mplighting.com | |
// how many time have you practice on pramp.com? since this March 120 | |
// "", "a*" // true/ false | |
// "", "a*.*" // true | |
// "","" // | |
// "a", "" // false | |
//"bbb", "b*b" | |
// how do you solve it? | |
// | |
// "aa", "aa" | |
// ^ ^ | |
// "abc", "a*c" | |
// ^ ^ | |
// "ac", "a*c" | |
// ^ ^ | |
/* | |
recursion tree | |
b* | |
/ \ \ \\ \\\ | |
"" b bb bbb bbbb bbbbb | |
match \ \ | |
continue | |
false | |
*/ | |
//"abc", "a.c" | |
//"abcd", "ab*bc." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment