Get Longest Match
Amazon is building a cutting-edge library for efficient string pattern matching. Your mission is to develop a service that detects the longest continuous segment within a provided string that aligns with a specified pattern.
You are given two inputs: a string sourceStr representing the main text, and a pattern string regexPattern containing one wildcard character (*). This wildcard can stand for any sequence of lowercase English letters, including an empty sequence. The pattern is considered a match if the wildcard can be substituted in a way that makes the pattern identical to a section of sourceStr
For instance, the pattern "abc*bcd" can match substrings like "abcbcd", "abcefgbcd", and "abccbcd", but it won't match "abcbd", "abzbcd", or "abcd".
Your goal is to find the longest matching substring in sourceStr that satisfies the pattern. Return its length, or -1 if no matching substring exists.
Complete the function getLongestMatch in the editor below.
getLongestMatch has the following parameters:
STRING sourceStr: a stringSTRING regexPattern: a stringReturns
int: the length of the longest substring that matches the regex, or -1 if there is no such substring.
🌷 🌿 Credit to Jane and txr 🌿 🌷
1Example 1
regex:
2Example 2
3Example 3
Constraints
Limits and guarantees your solution can rely on.