Q:
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
A:
The following solutions and code did not borrow any previous information, if there is a better solution please leave a comment in the comments section
The main idea of this problem is to achieve a match of a regular expression. There are two symbols "." Represents any single character "*" that represents 0 or more characters before it. I think I should be able to use the queue, the efficiency is good. Probably draw a diagram to show
The first thing to be sure is to meet. * This kind of coordination can certainly be returned directly, and a * as a character, or it can be said that when the comparison of characters, the next character of the regular expression is * does not return false and continue to judge. If you encounter a. Skip this judgment (you need to decide next time not *)
public class Regularexpressionmatching {public static void main (string[] args) {String s = "abc"; String reg = "ab*c*"; System.out.println (Method (S, reg));} Private static Boolean method (string s, String reg) {//TODO auto-generated method stubchar[] chars = S.tochararray (); Char [] Charreg = Reg.tochararray (); char charlast = 0;int RegIndex = 0;//reg cursor for (int i =0;i<s.length (); i++) {//out of Cursor range if (reg Index>=charreg.length) return false;if (charreg[regindex]== '. ') {//Get. Skip directly over charlast = charreg[regindex];regindex++;continue;} else if (charreg[regindex]== ' * ') {if (regindex!=0) {if (charlast== '. ') Point Star Match directly returns return True;else {//asterisk down match int j = i;for (; J<s.length (); j + +) {if (chars[j]!=charlast) break;} Charlast = Charreg[regindex];regindex++;i=--j;continue;}}} else {//Get character if (Chars[i]!=charreg[regindex]) {if (regindex< (charreg.length-1) &&charreg[regindex+1]== ' * ' ) {regindex+=2;continue;} return false;} if (regindex< (charreg.length-1) &&charreg[regindex+1]== ' * ') {charlast = Charreg[regindex];i--;regiNdex++;continue;} regindex++;} Charlast = Charreg[regindex];} if (regindex!=charreg.length)//Word length mismatch {if (Charreg.length>=regindex) {if ((regindex+1) ==charreg.length) {if ( charreg[regindex]!= ' * ') return False;elsereturn true; if (charreg[charreg.length-1]!= ' * ') return false;for (int i = regindex+2;i<charreg.length;i++) {///the remaining characters are. * or char* if (charreg[i-1]!= '. ') &&charreg[i-1]!= ' * ' &&charreg[i]!= '. ' &&charreg[i]!= ' * ') {return false;}}} return false;} return true;}
As a result of a stupid method, the bifurcation judgment more may be wrong, if wrong please inform, thank you ~ also because this code is messy, I may be followed by an improved version, let me think recently, refer to other contents.
[Leetcode] [10] Regular Expression Matching parsing-java implementation