Skip to content

Commit 38c03d2

Browse files
authored
Create odd-string-difference.cpp
1 parent 98a6ea0 commit 38c03d2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

C++/odd-string-difference.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(m * n), m is the number of words, n is the length of each word
2+
// Space: O(1)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
string oddString(vector<string>& words) {
8+
for (int i = 0; i < size(words[0]) - 1; ++i) {
9+
unordered_map<int, vector<int>> lookup;
10+
for (int j = 0; j < size(words); ++j) {
11+
if (size(lookup[words[j][i + 1] - words[j][i]]) < 2) {
12+
lookup[words[j][i + 1] - words[j][i]].emplace_back(j);
13+
}
14+
}
15+
if (size(lookup) == 2) {
16+
return size(cbegin(lookup)->second) == 1
17+
? words[cbegin(lookup)->second[0]]
18+
: words[next(cbegin(lookup))->second[0]];
19+
}
20+
}
21+
return "";
22+
}
23+
};

0 commit comments

Comments
 (0)