You are given a string s and a pattern string p, where p contains exactly two '*' characters.
The '*' in p matches any sequence of zero or more characters.
Return the length of the shortest substring in s that matches p. If there is no such substring, return -1.
Example 1:
Input: s = "abaacbaecebce", p = "ba*c*ce"
Output: 8
Explanation:
The shortest matching substring of p in s is "baecebce".
Example 2:
Input: s = "baccbaadbc", p = "cc*baa*adb"
Output: -1
Explanation:
There is no matching substring in s.
Example 3:
Input: s = "a", p = "**"
Output: 0
Explanation:
The empty substring is the shortest matching substring.
Example 4:
Input: s = "madlogic", p = "*adlogi*"
Output: 6
Explanation:
The shortest matching substring of p in s is "adlogi".
Constraints:
1 <= s.length <= 1052 <= p.length <= 105s contains only lowercase English letters.p contains only lowercase English letters and exactly two '*'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for finding the shortest matching substring involves checking every possible substring within the larger string to see if it contains the target string. We systematically explore all options, starting with the smallest possible substrings and increasing their size until a match is found. Then, we compare all the matching substrings to find the shortest one.
Here's how the algorithm would work step-by-step:
def shortest_matching_substring_brute_force(larger_string, target_string):
shortest_substring = None
# Iterate through all possible substring lengths
for substring_length in range(1, len(larger_string) + 1):
# Iterate through all possible start positions for the current length
for start_index in range(len(larger_string) - substring_length + 1):
substring = larger_string[start_index:start_index + substring_length]
# Check if the current substring contains the target string
if target_string in substring:
#If this is the first match, or shorter than prev
if shortest_substring is None or len(substring) < len(shortest_substring):
shortest_substring = substring
return shortest_substringThe goal is to find the smallest section of a big text that contains all the words from a smaller list of words. We achieve this efficiently by expanding a window and shrinking it whenever possible, avoiding unnecessary checks.
Here's how the algorithm would work step-by-step:
def find_shortest_matching_substring(text, words):
if not text or not words:
return ""
word_frequencies = {}
for word in words:
word_frequencies[word] = word_frequencies.get(word, 0) + 1
required_words_count = len(word_frequencies)
formed_words_count = 0
window_start = 0
window_end = 0
word_index_map = {}
min_length = float('inf')
min_start = 0
while window_end < len(text):
current_word = text[window_end]
if current_word in word_frequencies:
word_index_map[current_word] = word_index_map.get(current_word, 0) + 1
if word_index_map[current_word] == word_frequencies[current_word]:
formed_words_count += 1
# Shrink the window when all words are found
while formed_words_count == required_words_count:
if window_end - window_start + 1 < min_length:
min_length = window_end - window_start + 1
min_start = window_start
left_word = text[window_start]
#Only adjust if the leftmost word is in the target
if left_word in word_frequencies:
word_index_map[left_word] -= 1
#If no longer satisfied, decrement the count
if word_index_map[left_word] < word_frequencies[left_word]:
formed_words_count -= 1
window_start += 1
window_end += 1
if min_length == float('inf'):
return ""
# Return the shortest substring
return text[min_start:min_start + min_length]| Case | How to Handle |
|---|---|
| Null or empty source string | Return an empty string or null, indicating no matching substring found, and handle potential NullPointerException. |
| Null or empty target string | Return the original source string if the target is empty as every source string contains an empty string or throw an IllegalArgumentException if nulls are not allowed. |
| Target string longer than source string | Return an empty string or null, indicating no match is possible, since the target can't be a substring of the source. |
| Source string contains only target string characters | Return the shortest substring containing all target characters, even if the target appears elsewhere within it. |
| Target string contains duplicate characters | Ensure the solution correctly accounts for multiple occurrences of characters in the target when searching for a match. |
| Source string with multiple occurrences of target string | The solution should find the shortest substring, not necessarily the first occurrence. |
| No matching substring exists | Return an empty string or null if the source string does not contain all characters in the target string. |
| Very large strings (performance concerns) | Ensure the algorithm scales efficiently (ideally O(n) or O(n log n)) and avoids excessive memory allocation for large inputs by using sliding window and character frequency map. |