Skip to content

Commit 3054444

Browse files
authored
Create Odd String Difference.java
1 parent 46bd910 commit 3054444

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Easy/Odd String Difference.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String oddString(String[] words) {
3+
Map<String, Set<String>> diffCounter = new HashMap<>();
4+
for (String word : words) {
5+
String key = getDiffCounter(word);
6+
diffCounter.computeIfAbsent(key, k -> new HashSet<>()).add(word);
7+
}
8+
for (String key : diffCounter.keySet()) {
9+
if (diffCounter.get(key).size() == 1) {
10+
return diffCounter.get(key).iterator().next();
11+
}
12+
}
13+
return "";
14+
}
15+
16+
private static String getDiffCounter(String word) {
17+
StringBuilder sb = new StringBuilder();
18+
for (int i = 1; i < word.length(); i++) {
19+
sb.append(word.charAt(i) - word.charAt(i - 1)).append('|');
20+
}
21+
return sb.toString();
22+
}
23+
}

0 commit comments

Comments
 (0)