Problem:
Implement regular expression matching with support for ‘.‘
and ‘*‘
.
entire input string (not partial). The function prototype should be:bool IsMatch (const char *s, const char *p) Some examples:ismatch ("AA", "a") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true
Solution: A variety of methods to solve, can be used DFS, or DP, refer to Here: http://blog.csdn.net/hcbbt/article/details/44016237 topic: Given a string and a regular expression, Gives the idea of whether a regular match string is a problem: The key question on the * above, * can occupy 0 or more locations, so the way to use DFS is to try all the possible matches, such as Aaaaab and A*b, depends on Aaaaab and B and Aaab and B and AB and B
Java source code (spents 302ms):
public class Solution {public Boolean isMatch (string s, String p) { char[] chs = S.tochararray (); char[] CHP = P.tochararray (); Return Match (chs,0,chp,0); } public boolean Match (char[] chs,int index1,char[] Chp,int index2) { if (index2>=chp.length) return index1>= Chs.length; if (index2+1<chp.length && chp[index2+1]== ' * ') {while (index1<chs.length && (chp[index2]== '.' || CHP[INDEX2]==CHS[INDEX1]) { if (Match (chs,index1,chp,index2+2)) return true; index1++; } Return Match (chs,index1,chp,index2+2); } else if (index1<chs.length && (chp[index2]== '. ' | | chs[index1]==chp[index2])) { return Match (CHS, index1+1,chp,index2+1); } return false; }}
C Language Source code (spents 21ms):
BOOL IsMatch (char* S, char* p) { if (S==null | | p==null) return false; if (!*p) return!*s; if (* (p+1) = = ' * ') {while ((*p==*s) | | | (*s && *p== '. ')) { if (IsMatch (s,p+2)) return true; s++; } Return IsMatch (s,p+2); } else if ((*p==*s) | | | (*s && *p== '. ')) { return IsMatch (s+1,p+1); } return false;}
C + + source code (spents 407ms):
Class Solution {public: bool IsMatch (string s, String p) { return Match (s,0,p,0); } BOOL Match (string s,int index1,string p,int index2) { if (index2>=p.size ()) return index1>=s.size (); if (Index2+1<p.size () && p[index2+1]== ' * ') {while (Index1<s.size () && (p[index2]== '. ' | | | p[ INDEX2]==S[INDEX1]) { if (Match (s,index1,p,index2+2)) return true; index1++; } Return Match (s,index1,p,index2+2); } else if (Index1<s.size () && (p[index2]== '. ' | | p[index2]==s[index1])) { return Match (S,index1+1,p, index2+1); } return false; }};
Python source code (call self-function with 140ms):
Class solution: # @param {string} s # @param {string} p # @return {boolean} def isMatch (self, S, p): R Eturn re.match (' ^ ' + p + ' $ ', s)! = None
Python Source code (DFS timeout):
Class solution: # @param {string} s # @param {string} p # @return {boolean} def isMatch (self, S, p): R Eturn self. Match (s,0,p,0) def match (SELF,S,INDEX1,P,INDEX2): if Index2>=len (p): Return Index1>=len (s) if Index2+1<len (p) and p[index2+1]== ' * ': While Index1<len (s) and (p[index2]== '. ' or P[index2]==s[index1]): if self. Match (s,index1,p,index2+2): Return True index1+=1 return self. Match (s,index1,p,index2+2) elif Index1<len (s) and (p[index2]== '. ' or P[index2]==s[index1]): return self . Match (s,index1+1,p,index2+1) return False
Leetcode Regular Expression Matching (C,c++,java,python)