Maximum Distance Between Unequal Words in Array II
You are given an array of strings words.
Your task is to determine the maximum distance between two different indices i and j such that:
- words[i] != words[j]
- i < j
- Distance is defined as: distance = j - i + 1
Return the maximum possible distance among all valid pairs. If no such pair exists, return 0.
Example 1:
Input: words = ["apple", "banana", "banana", "cherry"]
Output: 4
Explanation:
- words[0] = "apple" and words[3] = "cherry" are different.
- Distance = 3 - 0 + 1 = 4, which is the maximum possible.
Example 2:
Input: words = ["one", "two", "one", "one", "three"]
Output: 5
Explanation:
- words[0] = "one" and words[4] = "three" are different.
- Distance = 4 - 0 + 1 = 5, which is the maximum possible.
Now Your Turn!
Pick the correct output for the given inputInput: words = ["cat", "cat", "dog", "cat"]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ words.length ≤ 105
- 1 ≤ words[i].length ≤ 10
- words[i] consists only of lowercase English letters.