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:

  1. Initialize two pointers p1 and p2 to store the positions of the two * characters in pattern p.
  2. Initialize variables matchStart and matchEnd to store the start and end indices of the current matching substring in s.
  3. Iterate over the string s:
    • If the character at the current index matches the character at position p1 or p2 in pattern p, update matchEnd to the current index.
    • If the characters don't match, update matchStart to the next index after the last * character.
    • Check if the current substring from matchStart to matchEnd matches the pattern p by considering the characters between the two * characters.
    • If it matches, update the length of the shortest matching substring if needed.
  4. 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.

Back to LeetCode Solutions