-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindDifference.java
More file actions
64 lines (56 loc) · 1.9 KB
/
findDifference.java
File metadata and controls
64 lines (56 loc) · 1.9 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public char findTheDifference(String s, String t) {
// approach 1 : hashmap :- make a hashmap and store characters of s string and check in t string which is not in hashmap so that character is added
HashMap<Character,Integer> map = new HashMap<>();
for(int i=0;i<s.length();i++){
char ch = s.charAt(i);
if(map.containsKey(ch)){
map.put(ch,map.get(ch)+1);
}else{
map.put(ch,1);
}
}
for(int i=0;i<t.length();i++){
char ch = t.charAt(i);
if(map.containsKey(ch)){
// Subtract the value from the existing value
int newValue = map.get(ch) - 1;
// Update the HashMap with the new value
map.put(ch , newValue);
if(map.get(ch)<0){
return ch;
}
}else{
return ch;
}
}
return 0;
// approach 2 : take the sum of all characters in ascii value s and t separately and than subtract t - s and return that result after converting in char
int sum_s = 0;
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
int asciiValue = (int) c;
sum_s += asciiValue;
}
int sum_t = 0;
for(int i=0;i<t.length();i++){
char c = t.charAt(i);
int asciiValue = (int) c;
sum_t += asciiValue;
}
int difference = sum_t - sum_s;
char ch = (char) difference;
return ch;
// approach 3 : using xor :-
int xor = 0;
for(int i=0;i<s.length();i++){
char ch = s.charAt(i);
xor ^= ch;
}
for(int i=0;i<t.length();i++){
char ch = t.charAt(i);
xor ^= ch;
}
return (char) xor;
}
}