#2451

Odd String Difference

pupil · 385 · lc easy +24 · verified · 61.8% accepted · 432 likes · top 62%

Description

You are given an array of equal-length strings words. The difference array of a string w of length n is a length-n - 1 integer array where entry j equals w[j+1] - w[j] (ordinal difference).

Find groups of strings in words that share the same difference array. Return a 2D array where each inner array lists strings from the same group in any order. The overall order of groups is also arbitrary.

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].

Code

1
2
3