3407. Substring Matching Pattern

3407. Substring Matching Pattern

Description

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:

1
2
3
Input: s = "leetcode", p = "ee*e"

Output: true

Explanation:

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

Example 2:

1
2
3
Input: s = "car", p = "c*v"

Output: false

Explanation:

There is no substring matching the pattern.

Example 3:

1
2
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 '*'

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
class Solution {
public:
bool hasMatch(const string& s, const string& p) {
int star = p.find('*');
int i = s.find(p.substr(0, star));
return i != string::npos && s.substr(i + star).find(p.substr(star + 1)) != string::npos;
}
};