Check if two strings differ by at most one character

Problem Determine whether two strings are "equal within one character" — identical, or fixable by exactly one substitution, insertion, or deletion (edit distance <= 1).

Input / Output

  • Input: strings a, b. Output: boolean.

Constraints

  • Lengths up to 10^5; O(n) single pass expected — full edit-distance DP is overkill.

Example

  • "cat"/"cut" → true (substitution); "cat"/"cats" → true (insertion); "cat"/"dog" → false; length gap ≥ 2 → false immediately.
asked …
LeaderboardSalaryAccount