LC.P2451[差值数组不同的字符串]

方法一:哈希表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public String oddString(String[] words) {
Map<String, List<String>> map = new HashMap<>();
int n = words[0].length();
for (String word : words) {
var cs = new char[n - 1];
for (int i = 0; i < n - 1; ++i) {
cs[i] = (char) (word.charAt(i + 1) - word.charAt(i));
}
String s = String.valueOf(cs);
map.computeIfAbsent(s, k -> new ArrayList<>()).add(word);
}
for (List<String> list : map.values()) {
if (list.size() == 1) return list.get(0);
}
return "";
}
}
  • 时间复杂度:$O(mn)$
  • 空间复杂度:$O(m + n)$

方法二:遍历

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
33
class Solution {
public String oddString(String[] words) {
int n = words.length;
int i = 1;
for (; i < n; ++i) {
if (!check(words[i], words[0])) {
break;
}
}

for (int j = 1; j < n; ++j) {
if (j != i) {
if (check(words[j], words[0])) {
return words[i];
} else {
return words[0];
}
}
}
return "";
}

private boolean check(String s1, String s2) {
int n = s1.length();
int m = s1.charAt(0) - s2.charAt(0);
for (int i = 1; i < n; i++) {
if (s1.charAt(i) - s2.charAt(i) != m) {
return false;
}
}
return true;
}
}
  • 时间复杂度:$O(mn)$
  • 空间复杂度:$O(n)$