【哈希】
class Solution {
public:
// 哈希 9:19 2
string f(string str) {
string res;
for (int i = 1; i < str.length(); i++) {
res += str[i] - str[i - 1];
}
return res;
}
string oddString(vector<string>& words) {
unordered_map<string, int> map;
for (auto w: words) map[f(w)]++;
for (auto w: words) {
if (map[f(w)] == 1) return w;
}
return "";
}
};
class Solution {
public String oddString(String[] words) {
Map<String, Integer> map = new HashMap();
Map<String, String> ans = new HashMap();
for (String w: words) {
StringBuilder sb = new StringBuilder();
int n = w.length();
for (int i = 1; i < n; i++) {
sb.append(String.valueOf(w.charAt(i) - w.charAt(i - 1)));
sb.append("#");
}
String str = sb.toString();
map.put(str, map.getOrDefault(str, 0) + 1);
ans.put(str, w);
}
for (var k: map.keySet()) {
if (map.get(k) == 1) return ans.get(k);
}
return "";
}
}