Interview 10 Big algorithm Rollup-strings and Arrays 3

Source: Internet
Author: User

6. Matching Regular expressions


Write the function Boolismatch (string s, String p) to complete the regular expression "." And the function of "*". Cases:

IsMatch ("AA", "a") return false

IsMatch ("AA", "AA") return True

IsMatch ("AAA", "AA") return False

IsMatch ("AA", "A *") return True

IsMatch ("AA", ". *") return True

IsMatch ("AB", ". *") return True

IsMatch ("AaB", "C*a*b") return True

Note: The first character of P is not *, and there is no two * connected in P

The problem can be divided into the following scenarios:

1. The length of P is 0: at this time the length of s must be 0 to match

2. The length of P is 1: If the length of S is less than 1, it must not match. Otherwise, if P.charat (0) is not "." Or not equal to S is not matched

3. Length of P >1:

Here are two possible considerations: I.p.charat (1)! = ' * '

Ii.p.charat (1) = = ' *

When P.charat (1)! = ' * ', the length of S is less than 1, it is bound to be mismatched. Compares the first character of S with P: if S is different from the first character of P and the first character of P is not ".", then it does not match. Otherwise iterate calls the function, return IsMatch (s.substring (1), p.substring (1))

When P.charat (1) = = ' * ',

。。。


PS: There is a problem with the code:

http://www.programcreek.com/2012/12/leetcode-regular-expression-matching-in-java/

For example, the following code test failed, temporarily did not think of the solution:

public class Test {public static Boolean IsMatch (string s, String p) {//Base Caseif (p.length () = = 0) {return s.length () = = 0;} Special Caseif (p.length () = = 1) {//If the length of S is 0, return Falseif (S.length () < 1) {return false;}//if The first does not match, return Falseelse if ((P.charat (0)! = S.charat (0)) && (P.charat (0)! = '. ')) {return false;} Otherwise, compare the rest of the string of S and P.else {return IsMatch (s.substring (1), p.substring (1));}} Case 1:when the second char of P was not ' * ' if (P.charat (1)! = ' * ') {if (S.length () < 1) {return false;} if ((P.charat (0)! = S.charat (0)) && (P.charat (0)! = '. ')) {return false;} else {return IsMatch (s.substring (1), p.substring (1));}} Case 2:when the second char of P was ' * ', complex case.else {//case 2.1:a char & ' * ' can stand for 0 elementif (is Match (S, p.substring (2))) {return true;}//case 2.2:a char & ' * ' can stand for 1 or more preceding element,//so try Every sub Stringinti = 0;while (I<s.length () && (S.charat (i) ==p.charat (0) | | P.charat (0) = = '. ')) {if (IsMatch (s.substring (i + 1), p.substring (2))) {return true;} i++;} return false;}} public static void Main (string[] args) {System.out.println (IsMatch ("12", "1*12"));}}



Interview 10 Big algorithm Rollup-strings and Arrays 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.