Skip to content

Commit 85b1118

Browse files
authored
Create 3696.Maximum-Distance-Between-Unequal-Words-in-Array-I.cpp
1 parent 6d86ce5 commit 85b1118

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maxDistance(vector<string>& words) {
4+
int n = words.size();
5+
if (words[0]!=words.back()) return n;
6+
7+
int ret = 0;
8+
for (int i=0; i<n; i++) {
9+
if (words[i]!=words[0])
10+
ret = max(ret, i-0+1);
11+
}
12+
13+
for (int i=n-2; i>=0; i--) {
14+
if (words[i]!=words.back())
15+
ret = max(ret, n-1-i+1);
16+
}
17+
18+
return ret;
19+
20+
}
21+
};

0 commit comments

Comments
 (0)