id
int64
1
3.71k
title
stringlengths
3
79
difficulty
stringclasses
3 values
description
stringlengths
430
25.4k
tags
stringlengths
0
131
language
stringclasses
19 values
solution
stringlengths
47
20.6k
3,706
Maximum Distance Between Unequal Words in Array II
Medium
<p>You are given a string array <code>words</code>.</p> <p>Find the <strong>maximum distance</strong> between two <strong>distinct</strong> indices <code>i</code> and <code>j</code> such that:</p> <ul> <li><code>words[i] != words[j]</code>, and</li> <li>the distance is defined as <code>j - i + 1</code>.</li> </ul> <p>Return the maximum distance among all such pairs. If no valid pair exists, return 0.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;leetcode&quot;,&quot;leetcode&quot;,&quot;codeforces&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>In this example, <code>words[0]</code> and <code>words[2]</code> are not equal, and they have the maximum distance <code>2 - 0 + 1 = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;a&quot;,&quot;a&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>In this example <code>words[1]</code> and <code>words[4]</code> have the largest distance of <code>4 - 1 + 1 = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;z&quot;,&quot;z&quot;,&quot;z&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p><strong>​​​​​​​</strong>In this example all the words are equal, thus the answer is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>5</sup></code><span style="display: none;"> </span></li> <li><code>1 &lt;= words[i].length &lt;= 10</code></li> <li><code>words[i]</code> consists of lowercase English letters.</li> </ul>
Python
class Solution: def maxDistance(self, words: List[str]) -> int: n = len(words) ans = 0 for i in range(n): if words[i] != words[0]: ans = max(ans, i + 1) if words[i] != words[-1]: ans = max(ans, n - i) return ans
3,706
Maximum Distance Between Unequal Words in Array II
Medium
<p>You are given a string array <code>words</code>.</p> <p>Find the <strong>maximum distance</strong> between two <strong>distinct</strong> indices <code>i</code> and <code>j</code> such that:</p> <ul> <li><code>words[i] != words[j]</code>, and</li> <li>the distance is defined as <code>j - i + 1</code>.</li> </ul> <p>Return the maximum distance among all such pairs. If no valid pair exists, return 0.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;leetcode&quot;,&quot;leetcode&quot;,&quot;codeforces&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>In this example, <code>words[0]</code> and <code>words[2]</code> are not equal, and they have the maximum distance <code>2 - 0 + 1 = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;a&quot;,&quot;a&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>In this example <code>words[1]</code> and <code>words[4]</code> have the largest distance of <code>4 - 1 + 1 = 4</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;z&quot;,&quot;z&quot;,&quot;z&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p><strong>​​​​​​​</strong>In this example all the words are equal, thus the answer is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>5</sup></code><span style="display: none;"> </span></li> <li><code>1 &lt;= words[i].length &lt;= 10</code></li> <li><code>words[i]</code> consists of lowercase English letters.</li> </ul>
TypeScript
function maxDistance(words: string[]): number { const n = words.length; let ans = 0; for (let i = 0; i < n; i++) { if (words[i] !== words[0]) { ans = Math.max(ans, i + 1); } if (words[i] !== words[n - 1]) { ans = Math.max(ans, n - i); } } return ans; }