Regular Expression Matching---Wrong Answer---"ab",".*c"


  • 1
    L

    Input: "ab", ".*c"

    Output: true

    Expected: false

    Why? I think ".*" means "........"(unlimited number) which can include any string.

    And in the example ——isMatch("ab", ".*") → true

    I was very confused.


  • 2
    L

    It's ".*c", so the first string should end with 'c'


  • 0
    L

    while, you need look at this example given in the subject.
    isMatch("aab", "c*a*b") → true.
    It means in the algorithm " isMatch(string s, string p)", p can include char which doesn't appear in s. Based on your comment, maybe the truth is if matching successed, p should end ???


  • 0
    M

    * Matches zero or more of the preceding element.
    So c* can matches empty string.


  • 0
    W

    for c* in the example isMatch("aab", "c*a*b") → true, * matches zero or more of the preceding element, but c still exist. So it equals to isMatch("aab", "ca*b") → true? Thanks!


  • 0
    L

    No. '*' Matches zero or more of the preceding element. Note "proceeding" which needs be adjacent.


  • 0
    W

    thanks, I finally understand what I have understood is wrong... Have a nice day~


  • 0
    Z

    I had the same confusion, and figured it out by enforcing the same end. I think this is really not clear in the question itself. p can have redundant sting on the left, but not on the right of s.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.