-
Notifications
You must be signed in to change notification settings - Fork 3
/
2451.cpp
32 lines (31 loc) · 969 Bytes
/
2451.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
bool isSame(vector<int>& arr1, vector<int>& arr2) {
int n = arr1.size();
for (int i = 0; i < n; ++i) {
if (arr1[i] != arr2[i]) return false;
}
return true;
}
string oddString(vector<string>& words) {
int m = words.size();
vector<pair<vector<int>, int>> diffArr;
for (int j = 0; j < m; ++j) {
string word = words[j];
int n = word.size();
vector<int> diffSub;
for (int i = 0; i < n - 1; ++i) {
int diff = word[i + 1] - word[i];
diffSub.push_back(diff);
}
diffArr.push_back({diffSub, j});
}
sort(diffArr.begin(), diffArr.end());
if (!isSame(diffArr[0].first, diffArr[1].first)) {
return words[diffArr[0].second];
}
else {
return words[diffArr[diffArr.size() - 1].second];
}
}
};