-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindTheDifference.java
51 lines (48 loc) · 1.36 KB
/
FindTheDifference.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package algorithm.topics.strings;
/**
* <a href="https://leetcode.cn/problems/find-the-difference/">Find the difference</a>
*
* @author marvin
* @version FindTheDifference.java, v 0.1 2022/10/18 00:31 Exp $
*/
public class FindTheDifference {
/**
* 首先遍历字符串s,对每个字符计数;然后再遍历字符串t,对每个字符减1,当出现负数时,当前字符即为答案
*
* @param s
* @param t
* @return
*/
public char findTheDifference(String s, String t) {
int[] cnt = new int[26];
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
cnt[ch - 'a']++;
}
for (int i = 0; i < t.length(); ++i) {
char ch = t.charAt(i);
cnt[ch - 'a']--;
if (cnt[ch - 'a'] < 0) {
return ch;
}
}
return ' ';
}
/**
* 位运算: 如果将两个字符串拼接成一个字符串,则问题转换成求字符串中出现奇数次的字符
*
* @param s
* @param t
* @return
*/
public char findTheDifference2(String s, String t) {
int ret = 0;
for (int i = 0; i < s.length(); ++i) {
ret ^= s.charAt(i);
}
for (int i = 0; i < t.length(); ++i) {
ret ^= t.charAt(i);
}
return (char) ret;
}
}