LeetCode 3455: Shortest Matching Substring Solution
Master LeetCode problem 3455 (Shortest Matching Substring), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3455. Shortest Matching Substring
Problem Explanation
Explanation:
To solve this problem, we can use a sliding window approach. We iterate over the string s and for each character we encounter, we check if it matches the pattern p. We keep track of the positions of the two * characters in the pattern p and use them to determine the window of characters in s that can match the pattern.
Algorithm:
- Initialize two pointers
p1andp2to store the positions of the two*characters in patternp. - Initialize variables
matchStartandmatchEndto store the start and end indices of the current matching substring ins. - Iterate over the string
s:- If the character at the current index matches the character at position
p1orp2in patternp, updatematchEndto the current index. - If the characters don't match, update
matchStartto the next index after the last*character. - Check if the current substring from
matchStarttomatchEndmatches the patternpby considering the characters between the two*characters. - If it matches, update the length of the shortest matching substring if needed.
- If the character at the current index matches the character at position
- Return the length of the shortest matching substring or -1 if no matching substring is found.
Time Complexity: O(n), where n is the length of string s.
Space Complexity: O(1)
:
Solution Code
class Solution {
public int shortestMatchingSubstring(String s, String p) {
int p1 = -1, p2 = -1;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*') {
if (p1 == -1) {
p1 = i;
} else {
p2 = i;
}
}
}
int matchStart = 0, matchEnd = 0;
int shortestLength = Integer.MAX_VALUE;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == p.charAt(p1) || s.charAt(i) == p.charAt(p2)) {
matchEnd = i;
} else {
matchStart = Math.max(matchStart, matchEnd + 1);
}
if (matchStart <= matchEnd && matchEnd < s.length() - 1) {
String substring = s.substring(matchStart, matchEnd + 1);
if (substring.contains(p.substring(p1 + 1, p2))) {
shortestLength = Math.min(shortestLength, matchEnd - matchStart + 1);
}
}
}
return shortestLength == Integer.MAX_VALUE ? -1 : shortestLength;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3455 (Shortest Matching Substring)?
This page provides optimized solutions for LeetCode problem 3455 (Shortest Matching Substring) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3455 (Shortest Matching Substring)?
The time complexity for LeetCode 3455 (Shortest Matching Substring) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3455 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3455 in Java, C++, or Python.