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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code