Get Longest Match
Learn this problemProblem statement
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.
Function
getLongestMatch(srcStr: String, regexPattern: String) → int
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 🌿 🌷
Examples
Example 1
srcStr = "hackerrank"regexPattern = "ack*r"return = 6regex:
Example 2
srcStr = "programming"regexPattern = "r*in"return = 9Example 3
srcStr = "debug"regexPattern = "ug*eb"return = -1Constraints
More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026