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.
"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.
Example 1:
Input: words = ["adc","wzy","abc"] Output: "abc" Explanation: - The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1]. - The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1]. - The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1]. The odd array out is [1, 1], so we return the corresponding string, "abc".
Example 2:
Input: words = ["aaa","bob","ccc","ddd"] Output: "bob" Explanation: All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
Constraints:
3 <= words.length <= 100n == words[i].length2 <= n <= 20words[i] consists of lowercase English letters.Problem Overview: You receive an array of equal‑length strings. For each word, compute the difference between adjacent characters (for example "abcd" → [1,1,1]). Most words share the same difference pattern, but one word has a different pattern. Return that odd string.
Approach 1: Divide and Conquer with Recursive Method (Time: O(n * m), Space: O(m) recursion + pattern storage)
This method recursively splits the array of words into halves and evaluates the difference pattern of representative strings from each segment. For each word, compute its difference array by iterating over characters and subtracting adjacent ASCII values. The recursion compares pattern groups from left and right halves to detect which side contains the outlier pattern. Once a mismatch is identified, the recursion continues only on that segment until the odd string is isolated. This approach demonstrates how divide‑and‑conquer can reduce comparisons when you only need to locate a single anomaly among otherwise identical patterns.
Approach 2: Dynamic Programming for Optimal Substructure (Time: O(n * m), Space: O(n * m))
A more direct solution builds a reusable representation of each word’s difference pattern. Iterate through the array once and compute a signature string such as "1#1#1" from adjacent character differences. Store these signatures in a hash map where the key is the pattern and the value is the list or count of words sharing it. Because almost all strings share the same pattern, the map quickly reveals the single pattern with frequency one. Dynamic programming ideas apply because each difference value is derived from previously processed characters and reused as part of the signature structure. Hash lookups run in constant time, making this solution straightforward and efficient for typical constraints.
Both approaches rely on iterating through characters and constructing a difference representation. Understanding how to derive these patterns from a string and efficiently group them with a hash table is the core skill. The input itself is processed sequentially as an array of words.
Recommended for interviews: The hash‑based pattern grouping approach (Approach 2). It’s concise, runs in linear time relative to the total characters processed (O(n * m)), and clearly shows your ability to transform strings into comparable signatures. Discussing the divide‑and‑conquer idea first can demonstrate problem‑solving depth, but interviewers usually expect the hash map solution.
This approach focuses on dividing the problem into smaller sub-problems (i.e., divide and conquer). We recursively solve each sub-problem, and combine their solutions to find the final solution. This technique is efficient for problems that can naturally be divided into similar sub-problems.
This C code demonstrates a basic divide and conquer method where the input array is divided into two halves, solved individually, and then combined appropriately. The specific problem's logic should be implemented in the combine function.
Time Complexity: O(n log n), where n is the number of elements.
Space Complexity: O(log n) due to recursion stack space.
This approach uses dynamic programming to take advantage of the optimal substructure property of the problem. By solving overlapping subproblems only once and storing their results, DP can optimize the solution process significantly.
This C code implements a dynamic programming approach that uses an array to store subproblem solutions. Adjust combination logic within the loop to suit specific problem requirements.
Time Complexity: O(n), iterating only through each element.
Space Complexity: O(n), storing solutions for each subproblem.
We use a hash table d to maintain the mapping relationship between the difference array of the string and the string itself, where the difference array is an array composed of the differences of adjacent characters in the string. Since the problem guarantees that except for one string, the difference arrays of other strings are the same, we only need to find the string with a different difference array.
The time complexity is O(m times n), and the space complexity is O(m + n). Here, m and n are the length of the string and the number of strings, respectively.
Rust
| Approach | Complexity |
|---|---|
| Approach 1: Divide and Conquer with Recursive Method | Time Complexity: O(n log n), where n is the number of elements. |
| Approach 2: Dynamic Programming for Optimal Substructure | Time Complexity: O(n), iterating only through each element. |
| Hash Table Simulation | — |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Divide and Conquer with Recursive Pattern Check | O(n * m) | O(m) to O(n * m) | When exploring recursive strategies to isolate anomalies in grouped data |
| Dynamic Programming with Hash Map Pattern Storage | O(n * m) | O(n * m) | General case; fastest and simplest method for detecting the unique difference pattern |
2451. Odd String Difference | Leetcode BiWeekly 90 | LeetCode 2451 • Bro Coders • 1,634 views views
Watch 9 more video solutions →Practice Odd String Difference with our built-in code editor and test cases.
Practice on FleetCode