-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFindTheDifference.java
More file actions
44 lines (41 loc) · 1.24 KB
/
FindTheDifference.java
File metadata and controls
44 lines (41 loc) · 1.24 KB
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
package hashtable;
// Source : https://leetcode.com/problems/find-the-difference/
// Id : 389
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-06-15
// Topic : Hashtable
// Level : Easy
// Other :
// Tips :
// Result : 99.37% 19.48%
public class FindTheDifference {
//99.37% 8.14%
public char findTheDifferenceOrigin(String s, String t) {
int[] count = new int[26];
char[] s1 = s.toCharArray();
char[] t1 = t.toCharArray();
for (int i = 0; i < s1.length; i++) {
count[t1[i] - 'a']++;
count[s1[i] - 'a']--;
}
System.out.println(count.toString());
for (int i = 0; i < count.length; i++) {
if (count[i] != 0)
return (char) ('a' + i);
}
return t1[t1.length - 1];
}
// 99.37% 19.48%
public char findTheDifference(String s, String t) {
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
count[t.charAt(i) - 'a']++;
count[s.charAt(i) - 'a']--;
}
for (int i = 0; i < count.length; i++) {
if (count[i] == 1)
return (char) ('a' + i);
}
return t.charAt(t.length() - 1);
}
}