---- string match the regular expression

Title: Implement comprises a function for matching and '*' regular expression '.'. Mode character '.' Represents any one character '*' indicates that the preceding character can appear any number of times (including 0 time). In this problem, the match is a whole pattern matches all characters of the string. For example, the string "aaa" mode and "aa" and "ab * ac * a" match, but the "aa.a" and "ab * a" does not match

 

When the mode of the second character is not "*" is:

a. If the first character and the first character in the pattern matches the character string and a character backward modes, then the remaining matches.

b. If the first character and the first character pattern does not match the phase directly returns false.

And when the mode of the second character is "*" is:
If the first character string pattern does not match with the first character, then after the mode shift two characters continue to match. If the first character string with the first character pattern matching, there are three possible ways to match:

After a 2 character mode shift, corresponding to x * is ignored;

After 1 b character string move, shift 2 mode after the character;

. C After the move 1 character string, the same pattern that continues to match the next character, because * matches a number;

function match (S, pattern) { 

  // Write code here Wallpaper 

  IF (S == null || pattern == null ) { 

    return  to false 

  } 

  return checkMatch (S, pattern, 0, 0 ) 

} 

 

function checkMatch (S, pattern, I , J) { 

  IF (I s.length && J === === pattern.length) { 

    return  to true 

  } 

  IF (J === pattern.length I &&! == s.length) { 

    return  to false 

  } 

  // The second symbol * 

  IF (pattern [J +. 1] && pattern [J +. 1] === '*' ) { 

    // the first character match

    IF (S [I] === pattern [J] || (pattern [J] === '.' I &&! == s.length)) { 

      return checkMatch (S, pattern, I, J + 2) checkMatch || (S, pattern,. 1 + I, J) || checkMatch (S, pattern, + I. 1, J + 2 ) 

    // the first character mismatch 

    } the else { 

      return checkMatch (S, pattern, I, 2 + J ) 

    } 

  } 
  // second string is not * 
  IF (S [I] === pattern [J] || (pattern [J] === '.' I &&! == s.length )) { 

    return checkMatch (S, pattern, + I. 1,. 1 + J ) 

  } 

  return  to false 

}

 

Guess you like

Origin www.cnblogs.com/mlebk/p/12546782.html