Skip to content

Commit 497464a

Browse files
authored
Merge pull request #1923 from Hemav009/395
Added 395
2 parents 4592943 + 1bb1ac5 commit 497464a

File tree

2 files changed

+443
-0
lines changed

2 files changed

+443
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
id: Find the Difference
3+
title: Find the Difference
4+
sidebar_label: 0389-Find the Difference
5+
tags:
6+
- Leet code
7+
description: "Solution to leetocde 389"
8+
---
9+
10+
### Problem Description
11+
12+
You are given two strings s and t.
13+
14+
String t is generated by random shuffling string s and then add one more letter at a random position.
15+
16+
Return the letter that was added to t.
17+
18+
Example 1:
19+
20+
```
21+
Input: s = "abcd", t = "abcde"
22+
Output: "e"
23+
Explanation: 'e' is the letter that was added.
24+
```
25+
26+
Example 2:
27+
28+
```
29+
Input: s = "", t = "y"
30+
Output: "y"
31+
```
32+
33+
### Constraints:
34+
35+
- $0 <= s.length <= 1000$
36+
- $t.length == s.length + 1$
37+
- $s and t consist of lowercase English letters.$
38+
39+
### Algorithm
40+
41+
1. For each character i in t, it checks if the count of i in t is greater than the count of i in s. If this condition is met, it means that i is the extra letter that was added to t, so it returns i and breaks out of the loop.
42+
43+
### Code Implementation
44+
45+
**Python:**
46+
47+
```python
48+
class Solution(object):
49+
def findTheDifference(self, s, t):
50+
for char in t:
51+
if t.count(char) > s.count(char):
52+
return char
53+
```
54+
55+
**C++:**
56+
57+
```c++
58+
#include <unordered_map>
59+
60+
char findTheDifference(std::string s, std::string t) {
61+
std::unordered_map<char, int> char_counts;
62+
for (char c : s) {
63+
char_counts[c]++;
64+
}
65+
for (char c : t) {
66+
if (char_counts.find(c) == char_counts.end() || char_counts[c] == 0) {
67+
return c;
68+
}
69+
char_counts[c]--;
70+
}
71+
return '\0'; // Return null character if no difference found
72+
}
73+
```
74+
75+
**Java:**
76+
77+
```java
78+
public class Solution {
79+
public char findTheDifference(String s, String t) {
80+
HashMap<Character, Integer> char_counts = new HashMap<>();
81+
for (char c : s.toCharArray()) {
82+
char_counts.put(c, char_counts.getOrDefault(c, 0) + 1);
83+
}
84+
for (char c : t.toCharArray()) {
85+
if (!char_counts.containsKey(c) || char_counts.get(c) == 0) {
86+
return c;
87+
}
88+
char_counts.put(c, char_counts.get(c) - 1);
89+
}
90+
return '\u0000'; // Return null character if no difference found (Unicode representation)
91+
}
92+
}
93+
```
94+
95+
**JavaScript:**
96+
97+
```javascript
98+
function findTheDifference(s, t) {
99+
const char_counts = {};
100+
for (const char of s) {
101+
char_counts[char] = (char_counts[char] || 0) + 1;
102+
}
103+
for (const char of t) {
104+
if (!(char in char_counts) || char_counts[char] === 0) {
105+
return char;
106+
}
107+
char_counts[char]--;
108+
}
109+
return ""; // Return empty string if no difference found
110+
}
111+
```

0 commit comments

Comments
 (0)