Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4ea5752

Browse files
committed
Add solution #389
1 parent 405f6ac commit 4ea5752

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

0389-find-the-difference.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 389. Find the Difference
3+
* https://leetcode.com/problems/find-the-difference/
4+
* Difficulty: Easy
5+
*
6+
* You are given two strings s and t.
7+
*
8+
* String t is generated by random shuffling string s and then add one more letter at
9+
* a random position.
10+
*
11+
* Return the letter that was added to t.
12+
*/
13+
14+
/**
15+
* @param {string} s
16+
* @param {string} t
17+
* @return {character}
18+
*/
19+
var findTheDifference = function(s, t) {
20+
const map = new Map();
21+
t.split('').forEach(c => map.set(c, (map.get(c) ?? 0) + 1));
22+
s.split('').forEach(c => map.set(c, map.get(c) - 1));
23+
return Array.from(map).find(([letter, count]) => count)[0];
24+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
374|[Guess Number Higher or Lower](./0374-guess-number-higher-or-lower.js)|Medium|
183183
383|[Ransom Note](./0383-ransom-note.js)|Easy|
184184
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
185+
389|[Find the Difference](./0389-find-the-difference.js)|Easy|
185186
392|[Is Subsequence](./0392-is-subsequence.js)|Easy|
186187
394|[Decode String](./0394-decode-string.js)|Medium|
187188
395|[Longest Substring with At Least K Repeating Characters](./0395-longest-substring-with-at-least-k-repeating-characters.js)|Medium|

0 commit comments

Comments
 (0)