File tree 4 files changed +59
-1
lines changed
4 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -562,3 +562,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
562
562
| 380. Insert Delete GetRandom O(1) | [ Link] ( https://leetcode.com/problems/insert-delete-getrandom-o1/ ) | [ Link] ( ./lib/medium/380_insert_delete_getrandom_o1.rb ) | [ Link] ( ./test/medium/test_380_insert_delete_getrandom_o1.rb ) |
563
563
| 382. Linked List Random Node | [ Link] ( https://leetcode.com/problems/linked-list-random-node/ ) | [ Link] ( ./lib/medium/382_linked_list_random_node.rb ) | [ Link] ( ./test/medium/test_382_linked_list_random_node.rb ) |
564
564
| 388. Longest Absolute File Path | [ Link] ( https://leetcode.com/problems/longest-absolute-file-path/ ) | [ Link] ( ./lib/medium/388_longest_absolute_file_path.rb ) | [ Link] ( ./test/medium/test_388_longest_absolute_file_path.rb ) |
565
+ | 394. Decode String | [ Link] ( https://leetcode.com/problems/decode-string/ ) | [ Link] ( ./lib/medium/394_decode_string.rb ) | [ Link] ( ./test/medium/test_394_decode_string.rb ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '6.9.6 '
8
+ s . version = '6.9.7 '
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ README.md ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ # https://leetcode.com/problems/decode-string/
4
+ # @param {String} s
5
+ # @return {String}
6
+ def decode_string ( s )
7
+ last = s . rindex ( '[' )
8
+
9
+ return s if last . nil?
10
+
11
+ p = last - 1
12
+ while p >= 0
13
+ break unless s [ p ] =~ /\d /
14
+
15
+ p -= 1
16
+ end
17
+
18
+ count = s [ p + 1 ..last - 1 ] . to_i
19
+ r_s = s [ last + 1 ...s . index ( ']' , last ) ] * count
20
+ s = s . sub ( s [ p + 1 ..s . index ( ']' , p + 1 ) ] , r_s )
21
+
22
+ decode_string ( s )
23
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper'
4
+ require_relative '../../lib/medium/394_decode_string'
5
+ require 'minitest/autorun'
6
+
7
+ class DecodeStringTest < ::Minitest ::Test
8
+ def test_default_one
9
+ assert_equal (
10
+ 'aaabcbc' ,
11
+ decode_string (
12
+ '3[a]2[bc]'
13
+ )
14
+ )
15
+ end
16
+
17
+ def test_default_two
18
+ assert_equal (
19
+ 'accaccacc' ,
20
+ decode_string (
21
+ '3[a2[c]]'
22
+ )
23
+ )
24
+ end
25
+
26
+ def test_default_three
27
+ assert_equal (
28
+ 'abcabccdcdcdef' ,
29
+ decode_string (
30
+ '2[abc]3[cd]ef'
31
+ )
32
+ )
33
+ end
34
+ end
You can’t perform that action at this time.
0 commit comments