We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 98a6ea0 commit 38c03d2Copy full SHA for 38c03d2
C++/odd-string-difference.cpp
@@ -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