Skip to content

Commit ba2fbd5

Browse files
authored
2025-01-08 v. 7.7.8: added "880. Decoded String at Index"
2 parents d16536c + 94eeae5 commit ba2fbd5

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
632632
| 863. All Nodes Distance K in Binary Tree | [Link](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | [Link](./lib/medium/863_all_nodes_distance_k_in_binary_tree.rb) | [Link](./test/medium/test_863_all_nodes_distance_k_in_binary_tree.rb) |
633633
| 865. Smallest Subtree with all the Deepest Nodes | [Link](https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/) | [Link](./lib/medium/865_smallest_subtree_with_all_the_deepest_nodes.rb) | [Link](./test/medium/test_865_smallest_subtree_with_all_the_deepest_nodes.rb) |
634634
| 869. Reordered Power of 2 | [Link](https://leetcode.com/problems/reordered-power-of-2/) | [Link](./lib/medium/869_reordered_power_of_2.rb) | [Link](./test/medium/test_869_reordered_power_of_2.rb) |
635+
| 880. Decoded String at Index | [Link](https://leetcode.com/problems/decoded-string-at-index/) | [Link](./lib/medium/880_decoded_string_at_index.rb) | [Link](./test/medium/test_880_decoded_string_at_index.rb) |
635636

636637
### Hard
637638

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.7.7'
8+
s.version = '7.7.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/decoded-string-at-index/
4+
# @param {String} s
5+
# @param {Integer} k
6+
# @return {String}
7+
def decode_at_index(s, k)
8+
size = 0
9+
10+
s.each_char do |c|
11+
if c =~ /[0-9]/
12+
size *= c.to_i
13+
else
14+
size += 1
15+
end
16+
end
17+
18+
s.each_char.reverse_each do |c|
19+
k %= size
20+
21+
return c if k.zero? && c =~ /[A-Za-z]/
22+
23+
if c =~ /[0-9]/
24+
size /= c.to_i
25+
else
26+
size -= 1
27+
end
28+
end
29+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/880_decoded_string_at_index'
5+
require 'minitest/autorun'
6+
7+
class DecodedStringAtIndexTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'o',
11+
decode_at_index(
12+
'leet2code3',
13+
10
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
'h',
21+
decode_at_index(
22+
'ha22',
23+
5
24+
)
25+
)
26+
end
27+
28+
def test_default_three
29+
assert_equal(
30+
'a',
31+
decode_at_index(
32+
'a2345678999999999999999',
33+
1
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)