Strings Differ by One Character - Problem

Given a list of strings dict where all the strings are of the same length.

Return true if there are 2 strings that only differ by 1 character in the same index, otherwise return false.

Note: Two strings differ by one character means exactly one position has different characters while all other positions have the same character.

Input & Output

Example 1 — Basic Case
$ Input: dict = ["abcd","acbd","aacd"]
Output: true
💡 Note: Strings "abcd" and "aacd" differ by exactly 1 character at index 1 (b vs a)
Example 2 — No Match
$ Input: dict = ["ab","cd","yz"]
Output: false
💡 Note: No two strings differ by exactly 1 character. Each pair differs by 2 characters
Example 3 — Multiple Differences
$ Input: dict = ["abcd","cccc","abcy","abcde"]
Output: true
💡 Note: Strings "abcd" and "abcy" differ by exactly 1 character at index 3 (d vs y)

Constraints

  • 1 ≤ dict.length ≤ 105
  • 1 ≤ dict[i].length ≤ 20
  • dict[i] consists of lowercase English letters only
  • All strings in dict have the same length

Visualization

Tap to expand
Strings Differ by One Character DetectionInput: ["abcd", "acbd", "aacd"]abcdacbdaacdString 1String 2String 3Compare: abcd vs aacdPosition 0: a = a ✓Position 1: b ≠ a ✗Position 2: c = c ✓Position 3: d = d ✓Exactly 1 difference found at position 1Output: true
Understanding the Visualization
1
Input
Array of strings with same length
2
Process
Find pairs differing by exactly 1 character
3
Output
Return true if such pair exists
Key Takeaway
🎯 Key Insight: Generate patterns by masking each position to efficiently find string pairs that differ by exactly one character
Asked in
Google 35 Facebook 28 Amazon 22
23.4K Views
Medium Frequency
~15 min Avg. Time
845 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