Find the Occurrence of First Almost Equal Substring - Problem
Find the First Almost Equal Substring

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
๐Ÿ•ต๏ธ Pattern Detective VisualizationSuspect Lineup (String s):abcdeSuspect Description (Pattern):bcdInvestigation Process:Position 0: Compare 'abc' with 'bcd'abcโŒ 2 differences > 1Position 1: Compare 'bcd' with 'bcd'bcdโœ… 0 differences โ‰ค 1๐ŸŽฏ Found Match at Position 1!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track differences and indices

n
2n
โœ“ 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
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22
38.2K Views
Medium-High Frequency
~18 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen