Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |

### Hard

Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '7.7.7'
s.version = '7.7.8'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
29 changes: 29 additions & 0 deletions lib/medium/880_decoded_string_at_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# https://leetcode.com/problems/decoded-string-at-index/
# @param {String} s
# @param {Integer} k
# @return {String}
def decode_at_index(s, k)
size = 0

s.each_char do |c|
if c =~ /[0-9]/
size *= c.to_i
else
size += 1
end
end

s.each_char.reverse_each do |c|
k %= size

return c if k.zero? && c =~ /[A-Za-z]/

if c =~ /[0-9]/
size /= c.to_i
else
size -= 1
end
end
end
37 changes: 37 additions & 0 deletions test/medium/test_880_decoded_string_at_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/880_decoded_string_at_index'
require 'minitest/autorun'

class DecodedStringAtIndexTest < ::Minitest::Test
def test_default_one
assert_equal(
'o',
decode_at_index(
'leet2code3',
10
)
)
end

def test_default_two
assert_equal(
'h',
decode_at_index(
'ha22',
5
)
)
end

def test_default_three
assert_equal(
'a',
decode_at_index(
'a2345678999999999999999',
1
)
)
end
end
Loading