Problem

You are given a string s and a pattern string p, where p contains exactly one '*' character.

The '*' in p can be replaced with any sequence of zero or more characters.

Return true if p can be made a substring of s, and false otherwise.

Example 1:

Input: s = “leetcode”, p = “ee*e”

Output: true

Explanation:

By replacing the '*' with "tcod", the substring "eetcode" matches the pattern.

Example 2:

Input: s = “car”, p = “c*v”

Output: false

Explanation:

There is no substring matching the pattern.

Example 3:

Input: s = “luck”, p = “u*”

Output: true

Explanation:

The substrings "u""uc", and "uck" match the pattern.

Constraints:

  • 1 <= s.length <= 50
  • 1 <= p.length <= 50
  • s contains only lowercase English letters.
  • p contains only lowercase English letters and exactly one '*'

Solution

Implementation

bool hasMatch(char* s, char* p) {
    char* token1 = strtok(p, "*");
    char* token2 = strtok(NULL, "*");
    if (token1 == NULL && token2 == NULL) return true;
    else if (token1 == NULL) {
        char* loc2 = strstr(s, token2);
        if (loc2 != NULL)
        return true;
    } 
    else if (token2 == NULL) {
        char *loc1 = strstr(s, token1);
        if (loc1 != NULL)
        return true;
    }
    else {
        printf("who");
        char *loc1 = strstr(s, token1);
        if (loc1 == NULL) return false;
        char *loc2 = strstr(loc1 + strlen(token1), token2);
        if (loc2 == NULL) return false;
        return true;
    }
    return false;
}