Skip to content

Commit 807a903

Browse files
add 2451
1 parent 1ae82de commit 807a903

File tree

3 files changed

+67
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+67
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2485.java) || Easy ||
5050
| 2467 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java) || Easy ||
5151
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java) || Easy ||
52+
| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2451.java) || Easy |
5253
| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2446.java) || Easy |
5354
| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2441.java) || Easy ||
5455
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2437.java) | | Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class _2451 {
9+
public static class Solution1 {
10+
public String oddString(String[] words) {
11+
Map<List<Integer>, List<String>> map = new HashMap<>();
12+
for (String word : words) {
13+
List<Integer> diffs = computeDiff(word);
14+
List<String> list = map.getOrDefault(diffs, new ArrayList<>());
15+
list.add(word);
16+
map.put(diffs, list);
17+
}
18+
for (Map.Entry<List<Integer>, List<String>> entry : map.entrySet()) {
19+
if (entry.getValue().size() == 1) {
20+
return entry.getValue().get(0);
21+
}
22+
}
23+
return null;
24+
}
25+
26+
private List<Integer> computeDiff(String word) {
27+
List<Integer> diffs = new ArrayList<>();
28+
for (int i = 0; i < word.length() - 1; i++) {
29+
diffs.add(word.charAt(i + 1) - word.charAt(i));
30+
}
31+
return diffs;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2451;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _2451Test {
10+
private static _2451.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2451.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("abc", solution1.oddString(new String[]{"adc", "wzy", "abc"}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("aaaabbbbbbaaabaaaabb", solution1.oddString(new String[]{"nnnmmmnnmmmmmmmmmmnm", "iiihhhiihhhhhhhhhhih", "aaaabbbbbbaaabaaaabb", "qqqpppqqppppppppppqp", "eeedddeedddddddddded", "eeedddeedddddddddded", "iiihhhiihhhhhhhhhhih", "lllkkkllkkkkkkkkkklk", "sssrrrssrrrrrrrrrrsr", "sssrrrssrrrrrrrrrrsr", "jjjiiijjiiiiiiiiiiji", "nnnmmmnnmmmmmmmmmmnm", "xxxwwwxxwwwwwwwwwwxw", "eeedddeedddddddddded", "zzzyyyzzyyyyyyyyyyzy", "wwwvvvwwvvvvvvvvvvwv", "cccbbbccbbbbbbbbbbcb", "xxxwwwxxwwwwwwwwwwxw", "cccbbbccbbbbbbbbbbcb", "yyyxxxyyxxxxxxxxxxyx", "hhhggghhgggggggggghg"}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("abb", solution1.oddString(new String[]{"mll", "abb", "edd", "jii", "tss", "fee", "dcc", "nmm", "utt", "zyy", "xww", "tss", "wvv", "xww", "utt"}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)