Easy
Odd String Difference — C++
Full explanation · Time O(m * n) · Space O(1)
// Time: O(m * n), m is the number of words, n is the length of each word
// Space: O(1)
// freq table
class Solution {
public:
string oddString(vector<string>& words) {
for (int i = 0; i < size(words[0]) - 1; ++i) {
unordered_map<int, vector<int>> lookup;
for (int j = 0; j < size(words); ++j) {
if (size(lookup[words[j][i + 1] - words[j][i]]) < 2) {
lookup[words[j][i + 1] - words[j][i]].emplace_back(j);
}
}
if (size(lookup) == 2) {
return size(cbegin(lookup)->second) == 1
? words[cbegin(lookup)->second[0]]
: words[next(cbegin(lookup))->second[0]];
}
}
return "";
}
};