-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindTheDifference.java
More file actions
29 lines (28 loc) · 896 Bytes
/
FindTheDifference.java
File metadata and controls
29 lines (28 loc) · 896 Bytes
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
package leetcode.array;
/**
* @ClassName FindTheDifference
* @Description 找不同 https://leetcode-cn.com/problems/find-the-difference/
* @Author changxuan
* @Date 2020/12/18 下午9:17
**/
public class FindTheDifference {
public static void main(String[] args) {
System.out.println(findTheDifference("abcd", "abcde"));
}
public static char findTheDifference(String s, String t) {
if (s.length() == 0) return t.toCharArray()[0];
int[] count = new int[26];
char[] sChar = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
count[sChar[i] - 97]++;
}
char[] tChar = t.toCharArray();
for (int i = 0; i < t.length(); i++) {
count[tChar[i] - 97]--;
}
for (int i = 0; i < 26; i++) {
if (count[i] == -1) return (char)(i+97);
}
return 'a';
}
}