Check Whether Two Search Queries Are Exactly One Edit Apart
Last updated: November 29, 2026
Quick Overview
Given two strings, return true if one can be turned into the other with exactly one insertion, deletion, or substitution of a single character.
0
0
0 solved
Given two strings, return true if one can be turned into the other with exactly one insertion, deletion, or substitution of a single character.
Framed around query spelling suggestions in Google Search, this question checks whether you avoid running full edit distance when a linear two-pointer scan suffices. Interviewers listen for a crisp case split on length difference and for attention to the exactly-one requirement.
What the Interviewer Expects
- Rejects immediately when lengths differ by more than one
- Uses a single two-pointer pass instead of O(nm) dynamic programming
- Treats identical strings as zero edits, so false
- Handles insertion and deletion symmetrically by swapping to make the first string shorter
- Tests edge cases like empty strings
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Generalize to at most k edits; what is the best complexity you can get?
- How would you find all dictionary words within one edit of a query quickly?
- What if transposition of adjacent characters also counts as one edit?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Inputs: two strings
a,bof arbitrary characters. - Return true only if the edit distance is exactly 1.
- Equal strings return false. Target O(n) time and O(1) extra space....
High-Level Design
If the lengths differ by more than 1, answer false. Make a the shorter (or equal) string. Walk both with indices i and j. On the first mismatch: if lengths are equal, it must be a substitution, ...