Odd String Difference - Problem

You are given an array of equal-length strings words. Assume that the length of each string is n.

Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25.

For example, for the string "acb", the difference integer array is [2 - 0, 1 - 2] = [2, -1].

All the strings in words have the same difference integer array, except one. You should find that string.

Return the string in words that has different difference integer array.

Input & Output

Example 1 — Basic Case
$ Input: words = ["abc", "bcd", "acb"]
Output: "acb"
💡 Note: "abc" → [1,1] (b-a=1, c-b=1), "bcd" → [1,1] (c-b=1, d-c=1), "acb" → [2,-1] (c-a=2, b-c=-1). The string "acb" has a different pattern.
Example 2 — Different Position
$ Input: words = ["adc", "wzy", "abc"]
Output: "adc"
💡 Note: "adc" → [3,-1] (d-a=3, c-d=-1), "wzy" → [-3,1] (z-w=-3, y-z=1), "abc" → [1,1]. The string "adc" has a unique difference pattern.
Example 3 — Edge Case
$ Input: words = ["aaa", "bob", "ccc"]
Output: "bob"
💡 Note: "aaa" → [0,0] (a-a=0, a-a=0), "bob" → [13,-13] (o-b=13, b-o=-13), "ccc" → [0,0]. The string "bob" is the only one with non-zero differences.

Constraints

  • 3 ≤ words.length ≤ 100
  • n == words[i].length
  • 2 ≤ n ≤ 20
  • words[i] consists of lowercase English letters.

Visualization

Tap to expand
Odd String Difference: Find the Unique PatternString 1:"abc"String 2:"bcd"String 3:"acb"Difference:[1, 1]Difference:[1, 1]Difference:[2, -1]b-a=1, c-b=1 → [1,1]c-b=1, d-c=1 → [1,1]c-a=2, b-c=-1 → [2,-1] ← UNIQUEAnswer: "acb"
Understanding the Visualization
1
Input Strings
Array of equal-length strings
2
Difference Arrays
Convert each string to consecutive character differences
3
Find Unique
Identify the string with different pattern
Key Takeaway
🎯 Key Insight: Only one string has a different consecutive character difference pattern - find it by comparing difference arrays efficiently.
Asked in
Google 15 Microsoft 12 Amazon 8
12.5K Views
Medium Frequency
~12 min Avg. Time
245 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