Class Solution

java.lang.Object
g2401_2500.s2451_odd_string_difference.Solution

public class Solution extends Object
2451 - Odd String Difference.<p>Easy</p> <p>You are given an array of equal-length strings <code>words</code>. Assume that the length of each string is <code>n</code>.</p> <p>Each string <code>words[i]</code> can be converted into a <strong>difference integer array</strong> <code>difference[i]</code> of length <code>n - 1</code> where <code>difference[i][j] = words[i][j+1] - words[i][j]</code> where <code>0 <= j <= n - 2</code>. Note that the difference between two letters is the difference between their <strong>positions</strong> in the alphabet i.e. the position of <code>'a'</code> is <code>0</code>, <code>'b'</code> is <code>1</code>, and <code>'z'</code> is <code>25</code>.</p> <ul> <li>For example, for the string <code>&quot;acb&quot;</code>, the difference integer array is <code>[2 - 0, 1 - 2] = [2, -1]</code>.</li> </ul> <p>All the strings in words have the same difference integer array, <strong>except one</strong>. You should find that string.</p> <p>Return <em>the string in</em> <code>words</code> <em>that has different <strong>difference integer array</strong>.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;adc&rdquo;,&ldquo;wzy&rdquo;,&ldquo;abc&rdquo;]</p> <p><strong>Output:</strong> &ldquo;abc&rdquo;</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>The difference integer array of &ldquo;adc&rdquo; is [3 - 0, 2 - 3] = [3, -1].</p> </li> <li> <p>The difference integer array of &ldquo;wzy&rdquo; is [25 - 22, 24 - 25]= [3, -1].</p> </li> <li> <p>The difference integer array of &ldquo;abc&rdquo; is [1 - 0, 2 - 1] = [1, 1].</p> </li> </ul> <p>The odd array out is [1, 1], so we return the corresponding string, &ldquo;abc&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;aaa&rdquo;,&ldquo;bob&rdquo;,&ldquo;ccc&rdquo;,&ldquo;ddd&rdquo;]</p> <p><strong>Output:</strong> &ldquo;bob&rdquo;</p> <p><strong>Explanation:</strong> All the integer arrays are [0, 0] except for &ldquo;bob&rdquo;, which corresponds to [13, -13].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 <= words.length <= 100</code></li> <li><code>n == words[i].length</code></li> <li><code>2 <= n <= 20</code></li> <li><code>words[i]</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details