Find the Occurrence of First Almost Equal Substring - Problem
Find the First Almost Equal Substring
You're given two strings
๐ What does "almost equal" mean?
Two strings are almost equal if you can change at most one character in one string to make it identical to the other. This means:
โข Strings can be exactly equal (0 changes needed)
โข Strings can differ by exactly 1 character (1 change needed)
๐ Goal: Return the smallest starting index where such a substring exists, or
Example: If
You're given two strings
s and pattern. Your task is to find the smallest starting index of a substring in s that is almost equal to the pattern.๐ What does "almost equal" mean?
Two strings are almost equal if you can change at most one character in one string to make it identical to the other. This means:
โข Strings can be exactly equal (0 changes needed)
โข Strings can differ by exactly 1 character (1 change needed)
๐ Goal: Return the smallest starting index where such a substring exists, or
-1 if no such substring is found.Example: If
s = "abcdef" and pattern = "bcd", the substring starting at index 1 is exactly equal to the pattern, so return 1. Input & Output
example_1.py โ Basic Match
$
Input:
s = "leetcode", pattern = "eet"
โบ
Output:
1
๐ก Note:
The substring starting at index 1 is "eet" which exactly matches the pattern (0 differences โค 1)
example_2.py โ Almost Equal Match
$
Input:
s = "abcdef", pattern = "axd"
โบ
Output:
0
๐ก Note:
The substring starting at index 0 is "abc". Comparing with "axd": 'a'='a' (match), 'b'โ 'x' (diff), 'c'โ 'd' (diff). Total 2 differences > 1, so check next position. At index 1: "bcd" vs "axd" has 3 differences. Continue... Actually, no valid match exists, should return -1
example_3.py โ No Match Found
$
Input:
s = "abc", pattern = "def"
โบ
Output:
-1
๐ก Note:
The only possible substring is "abc" which differs from "def" in all 3 positions (3 > 1), so return -1
Visualization
Tap to expand
Understanding the Visualization
1
Start the Investigation
Begin at the first person in the lineup (index 0)
2
Feature Comparison
Compare each feature (character) with the description (pattern)
3
Count Discrepancies
Keep track of how many features don't match
4
Make Decision
If โค1 feature differs, we found our match! Otherwise, move to next person
5
Report Results
Return the position of first match, or -1 if no match found
Key Takeaway
๐ฏ Key Insight: We systematically check each position from left to right, counting character differences. The first position with โค1 differences is our answer, ensuring we find the smallest valid index.
Time & Space Complexity
Time Complexity
O(n*m)
We check at most (n-m+1) starting positions, and for each position we compare m characters
โ Linear Growth
Space Complexity
O(1)
Only using a few variables to track differences and indices
โ Linear Space
Constraints
- 1 โค pattern.length โค s.length โค 105
- s and pattern consist only of lowercase English letters
- Return the smallest index if multiple valid positions exist
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code