File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 186
186
| 382 | Linked List Random Node | [ Ruby] ( ./algorithms/ruby/0382-linked-list-random-node.rb ) | Medium |
187
187
| 383 | Ransom Note | [ Ruby] ( ./algorithms/ruby/0383-ransom-note.rb ) | Easy |
188
188
| 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 |
189
190
| 392 | Is Subsequence | [ Ruby] ( ./algorithms/ruby/0392-is-subsequence.rb ) | Easy |
190
191
| 394 | Decode String | [ Ruby] ( ./algorithms/ruby/0394-decode-string.rb ) | Medium |
191
192
| 399 | Evaluate Division | [ Ruby] ( ./algorithms/ruby/0399-evaluate-division.rb ) | Medium |
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments