Skip to content

Commit 813f607

Browse files
committed
2024-12-06 v. 7.2.5: added "535. Encode and Decode TinyURL"
1 parent a21cc17 commit 813f607

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
590590
| 516. Longest Palindromic Subsequence | [Link](https://leetcode.com/problems/longest-palindromic-subsequence/) | [Link](./lib/medium/516_longest_palindromic_subsequence.rb) | [Link](./test/medium/test_516_longest_palindromic_subsequence.rb) |
591591
| 523. Continuous Subarray Sum | [Link](https://leetcode.com/problems/continuous-subarray-sum/) | [Link](./lib/medium/523_continuous_subarray_sum.rb) | [Link](./test/medium/test_523_continuous_subarray_sum.rb) |
592592
| 524. Longest Word in Dictionary through Deleting | [Link](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/) | [Link](./lib/medium/524_longest_word_in_dictionary_through_deleting.rb) | [Link](./test/medium/test_524_longest_word_in_dictionary_through_deleting.rb) |
593+
| 535. Encode and Decode TinyURL | [Link](https://leetcode.com/problems/encode-and-decode-tinyurl/) | [Link](./lib/medium/535_encode_and_decode_tinyurl.rb) | [Link](./test/medium/test_535_encode_and_decode_tinyurl.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.2.4'
8+
s.version = '7.2.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
$urls = {}
4+
5+
# https://leetcode.com/problems/encode-and-decode-tinyurl/
6+
# @param {String} long_url
7+
# @return {String}
8+
def encode(long_url)
9+
short_url = generate_short_url(long_url)
10+
short_url = generate_short_url(long_url) while $urls.include?(short_url)
11+
12+
$urls[short_url] = long_url
13+
14+
short_url
15+
end
16+
17+
# @param {String} long_url
18+
# @return {String}
19+
def generate_short_url(_long_url)
20+
symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
21+
22+
short_url = []
23+
(0...6).each { |_| short_url << symbols[rand(symbols.size)] }
24+
25+
short_url.join
26+
end
27+
28+
# @param {String} short_url
29+
# @return {String}
30+
def decode(short_url) = $urls[short_url]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/535_encode_and_decode_tinyurl'
5+
require 'minitest/autorun'
6+
7+
class EncodeAndDecodeTinyURLTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'https://leetcode.com/problems/design-tinyurl',
11+
decode(
12+
encode(
13+
'https://leetcode.com/problems/design-tinyurl'
14+
)
15+
)
16+
)
17+
end
18+
end

0 commit comments

Comments
 (0)