Odd String Difference - Problem

You're given an array of equal-length strings where all strings follow the same character pattern except one. Your mission is to identify the odd string out!

How it works:

  • Each string can be converted into a difference array by calculating the difference between adjacent characters
  • The difference between two letters is their position difference in the alphabet: 'a'=0, 'b'=1, ..., 'z'=25
  • For example: "acb" → differences are [2-0, 1-2] = [2, -1]

The twist: All strings will have the same difference array except exactly one. Find that unique string!

Goal: Return the string that has a different difference pattern from all others.

Input & Output

example_1.py — Basic Example
$ Input: ["adc", "wzy", "abc"]
Output: "abc"
💡 Note: "adc" → [2, -1], "wzy" → [2, -1], "abc" → [1, 1]. The strings "adc" and "wzy" have the same difference pattern [2, -1], while "abc" has a unique pattern [1, 1].
example_2.py — Different Positions
$ Input: ["aaa", "bob", "ccc", "ddd"]
Output: "bob"
💡 Note: "aaa" → [0, 0], "bob" → [13, -13], "ccc" → [0, 0], "ddd" → [0, 0]. Three strings have pattern [0, 0] while "bob" has unique pattern [13, -13].
example_3.py — Edge Case
$ Input: ["xy", "yz", "zx"]
Output: "zx"
💡 Note: "xy" → [1], "yz" → [1], "zx" → [-23]. Two strings have pattern [1] while "zx" has unique pattern [-23] (z to x wraps around the alphabet).

Constraints

  • 3 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 20
  • words[i] consists of lowercase English letters only
  • All strings have the same length
  • Exactly one string has a different difference pattern

Visualization

Tap to expand
DNA-1"adc"[2, -1]DNA-2"wzy"[2, -1]DNA-3"abc"[1, 1]Pattern Analysis Lab[2, -1]: Count 2[1, 1]: Count 1🧬 MUTANT DETECTED!RESULT"abc"
Understanding the Visualization
1
Extract DNA Signatures
Convert each DNA sequence to its characteristic difference pattern
2
Count Pattern Frequencies
Use a hash map to count how many sequences have each pattern
3
Identify the Mutant
The pattern with frequency 1 belongs to the mutant sequence
Key Takeaway
🎯 Key Insight: By counting pattern frequencies, we can identify the unique string in O(n×m) time - much more efficient than comparing every pair!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
18.4K Views
Medium Frequency
~15 min Avg. Time
847 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