Skip to content

Commit d2f9f40

Browse files
realDuYuanChaogithub-actions
andauthored
find the difference (examplehub#149)
* find the difference * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent d958d0b commit d2f9f40

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
/** https://leetcode.com/problems/find-the-difference/ */
7+
public class FindTheDifference {
8+
public static char solution1(String s, String t) {
9+
char[] firstChars = s.toCharArray();
10+
char[] secondChars = t.toCharArray();
11+
Arrays.sort(firstChars);
12+
Arrays.sort(secondChars);
13+
for (int i = 0; i < firstChars.length; ++i) {
14+
if (firstChars[i] != secondChars[i]) {
15+
return secondChars[i];
16+
}
17+
}
18+
return secondChars[secondChars.length - 1];
19+
}
20+
21+
public static char solution2(String s, String t) {
22+
int[] countTab = new int[26];
23+
for (char ch : s.toCharArray()) {
24+
countTab[ch - 'a']++;
25+
}
26+
for (char ch : t.toCharArray()) {
27+
countTab[ch - 'a']--;
28+
if (countTab[ch - 'a'] < 0) {
29+
return ch;
30+
}
31+
}
32+
return ' ';
33+
}
34+
35+
public static char solution3(String s, String t) {
36+
int sum = 0;
37+
for (char ch : t.toCharArray()) {
38+
sum += ch;
39+
}
40+
for (char ch : s.toCharArray()) {
41+
sum -= ch;
42+
}
43+
return (char) sum;
44+
}
45+
46+
public static char solution4(String s, String t) {
47+
int ret = 0;
48+
for (int i = 0; i < s.length(); ++i) {
49+
ret ^= s.charAt(i);
50+
}
51+
for (int i = 0; i < t.length(); ++i) {
52+
ret ^= t.charAt(i);
53+
}
54+
return (char) ret;
55+
}
56+
57+
public static char solution5(String s, String t) {
58+
s = s + t;
59+
HashSet<Character> set = new HashSet<>();
60+
for (char ch : s.toCharArray()) {
61+
if (set.contains(ch)) {
62+
set.remove(ch);
63+
} else {
64+
set.add(ch);
65+
}
66+
}
67+
for (char ch : set) {
68+
return ch;
69+
}
70+
return ' ';
71+
}
72+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class FindTheDifferenceTest {
8+
@Test
9+
void testSolution1() {
10+
assertEquals('e', FindTheDifference.solution1("abcd", "abcde"));
11+
assertEquals('y', FindTheDifference.solution1("", "y"));
12+
assertEquals('a', FindTheDifference.solution1("a", "aa"));
13+
assertEquals('a', FindTheDifference.solution1("ae", "aea"));
14+
}
15+
16+
@Test
17+
void testSolution2() {
18+
assertEquals('e', FindTheDifference.solution2("abcd", "abcde"));
19+
assertEquals('y', FindTheDifference.solution2("", "y"));
20+
assertEquals('a', FindTheDifference.solution2("a", "aa"));
21+
assertEquals('a', FindTheDifference.solution2("ae", "aea"));
22+
}
23+
24+
@Test
25+
void testSolution3() {
26+
assertEquals('e', FindTheDifference.solution3("abcd", "abcde"));
27+
assertEquals('y', FindTheDifference.solution3("", "y"));
28+
assertEquals('a', FindTheDifference.solution3("a", "aa"));
29+
assertEquals('a', FindTheDifference.solution3("ae", "aea"));
30+
}
31+
32+
@Test
33+
void testSolution4() {
34+
assertEquals('e', FindTheDifference.solution4("abcd", "abcde"));
35+
assertEquals('y', FindTheDifference.solution4("", "y"));
36+
assertEquals('a', FindTheDifference.solution4("a", "aa"));
37+
assertEquals('a', FindTheDifference.solution4("ae", "aea"));
38+
}
39+
40+
@Test
41+
void testSolution5() {
42+
assertEquals('e', FindTheDifference.solution5("abcd", "abcde"));
43+
assertEquals('y', FindTheDifference.solution5("", "y"));
44+
assertEquals('a', FindTheDifference.solution5("a", "aa"));
45+
assertEquals('a', FindTheDifference.solution5("ae", "aea"));
46+
}
47+
}

0 commit comments

Comments
 (0)