Skip to content

Commit eeb9634

Browse files
committed
Find the Difference
1 parent b9c3171 commit eeb9634

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 25- Find the Difference
2+
3+
The problem can be found at the following link: [Question Link](https://leetcode.com/problems/find-the-difference/description)
4+
5+
6+
7+
### My Approach
8+
9+
10+
1. Initialize an unordered_map called `mp` to store character frequencies.
11+
2. Iterate through each character `c` in string `s`:
12+
- Increment the count of character `c` in the `mp` map by 1.
13+
- This loop populates the `mp` map with the frequency of each character in string `s`.
14+
15+
3. Iterate through each character `c` in string `t`:
16+
- Decrement the count of character `c` in the `mp` map by 1.
17+
- Check if the count becomes less than 0. If it does, return the character `c`. This character is the one that appears more times in string `t` than in string `s`, making it the "difference" character.
18+
19+
4. If no difference character is found during the loop in step 3, return the null character `'\0'`. This indicates that all characters in `s` and `t` are the same, and there is no additional character in `t`.
20+
21+
22+
23+
### Time and Auxiliary Space Complexity
24+
25+
- Time Complexity: `O(n)`
26+
- Auxiliary Space Complexity: `O(n)`
27+
28+
29+
30+
### Code (C++)
31+
32+
```cpp
33+
34+
class Solution {
35+
public:
36+
char findTheDifference(string s, string t) {
37+
unordered_map<char,int> mp;
38+
for(auto& c:s) mp[c]+=1;
39+
for(auto& c:t) if(--mp[c]<0) return c;
40+
return '\0';
41+
}
42+
};
43+
44+
```
45+
46+
### Contribution and Support
47+
48+
For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://leetcode.com/discuss/general-discussion). We welcome your input and aim to foster a collaborative learning environment.
49+
50+
If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [rishabhv12/Daily-Leetcode-Solution](https://github.com/rishabhv12/Daily-Leetcode-Solution) repository.

0 commit comments

Comments
 (0)