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.
Example 1:
Input: dict = ["abcd","acbd", "aacd"] Output: true Explanation: Strings "abcd" and "aacd" differ only by one character in the index 1.
Example 2:
Input: dict = ["ab","cd","yz"] Output: false
Example 3:
Input: dict = ["abcd","cccc","abyd","abab"] Output: true
Constraints:
dict <= 105dict[i].length == dict[j].lengthdict[i] should be unique.dict[i] contains only lowercase English letters.Problem Overview: You receive an array of equal-length strings. The task is to determine whether any two strings differ by exactly one character at the same index. If such a pair exists, return true. Otherwise return false.
Approach 1: Pairwise Comparison (Brute Force) (O(n² * m) time, O(1) space)
Compare every pair of strings and count how many positions differ. For each pair, iterate through all m characters and increment a mismatch counter. If the count equals one, the condition is satisfied and you return true. This approach is simple but expensive because it performs comparisons across all n² pairs and scans each string fully.
Approach 2: Hashing by Removing One Character (O(n * m) time, O(n) space)
Instead of comparing every pair, iterate through each character index and simulate removing that position from every string. Build a modified version of each string that skips the current index. Store these modified strings in a HashSet. If two strings become identical after removing the same index, they must differ by exactly one character at that position. This reduces pairwise comparisons and leverages fast hash lookups. The approach relies heavily on efficient Hash Table operations and string processing.
Approach 3: Rolling Hash with Character Removal (Optimal) (O(n * m) time, O(n) space)
Instead of rebuilding strings, compute a polynomial rolling hash for each string. Precompute powers for the hash base. For every index i, adjust each string's hash by subtracting the contribution of the character at position i. The resulting hash represents the string with that character removed. Insert these hashes into a set and check for duplicates. Hash comparison avoids constructing new strings, which improves constant factors and memory behavior. This technique combines string manipulation with rolling hash techniques used in substring matching algorithms.
Recommended for interviews: The rolling hash solution is the expected optimal approach because it reduces the naive quadratic comparison into linear passes over the data. Showing the brute force approach first demonstrates understanding of the requirement, but implementing the hash-based method shows stronger algorithmic reasoning and familiarity with hashing optimizations.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Pairwise Comparison (Brute Force) | O(n² * m) | O(1) | Small datasets or when implementing the simplest correctness check |
| Hashing by Removing One Character | O(n * m) | O(n) | When string reconstruction cost is acceptable and implementation simplicity is preferred |
| Rolling Hash Character Removal | O(n * m) | O(n) | Best general solution with efficient hashing and minimal string copying |
1554. Strings Differ by One Character • Muerde un zombie • 1,502 views views
Watch 1 more video solutions →Practice Strings Differ by One Character with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor