Skip to content

Commit b5a82ea

Browse files
committed
389. Find the Difference
1 parent 271a8a9 commit b5a82ea

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
| 382 | Linked List Random Node | [Ruby](./algorithms/ruby/0382-linked-list-random-node.rb) | Medium |
187187
| 383 | Ransom Note | [Ruby](./algorithms/ruby/0383-ransom-note.rb) | Easy |
188188
| 387 | First Unique Character in a String | [Ruby](./algorithms/ruby/0387-first-unique-character-in-a-string.rb) | Easy |
189+
| 389 | Find the Difference | [Ruby](./algorithms/ruby/0389-find-the-difference.rb) | Easy |
189190
| 392 | Is Subsequence | [Ruby](./algorithms/ruby/0392-is-subsequence.rb) | Easy |
190191
| 394 | Decode String | [Ruby](./algorithms/ruby/0394-decode-string.rb) | Medium |
191192
| 399 | Evaluate Division | [Ruby](./algorithms/ruby/0399-evaluate-division.rb) | Medium |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# 389. Find the Difference
4+
# Easy
5+
# https://leetcode.com/problems/find-the-difference
6+
7+
=begin
8+
You are given two strings s and t.
9+
10+
String t is generated by random shuffling string s and then add one more letter at a random position.
11+
Return the letter that was added to t.
12+
13+
Example 1:
14+
Input: s = "abcd", t = "abcde"
15+
Output: "e"
16+
Explanation: 'e' is the letter that was added.
17+
18+
Example 2:
19+
Input: s = "", t = "y"
20+
Output: "y"
21+
22+
Constraints:
23+
0 <= s.length <= 1000
24+
t.length == s.length + 1
25+
s and t consist of lowercase English letters.
26+
=end
27+
28+
# @param {String} s
29+
# @param {String} t
30+
# @return {Character}
31+
def find_the_difference(s, t)
32+
(s + t).bytes.reduce(:^).chr
33+
end
34+
35+
# **************** #
36+
# TEST #
37+
# **************** #
38+
39+
require "test/unit"
40+
class Test_find_the_difference < Test::Unit::TestCase
41+
def test_
42+
assert_equal "e", find_the_difference("abcd", "abcde")
43+
assert_equal "y", find_the_difference("", "y")
44+
end
45+
end

0 commit comments

Comments
 (0)