Skip to content

2024-05-13 v. 5.6.6: added "2399. Check Distances Between Same Letters" #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2024
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 @@ -426,3 +426,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2383. Minimum Hours of Training to Win a Competition | [Link](https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/) | [Link](./lib/easy/2383_minimum_hours_of_training_to_win_a_competition.rb) |
| 2389. Longest Subsequence With Limited Sum | [Link](https://leetcode.com/problems/longest-subsequence-with-limited-sum/) | [Link](./lib/easy/2389_longest_subsequence_with_limited_sum.rb) |
| 2395. Find Subarrays With Equal Sum | [Link](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [Link](./lib/easy/2395_find_subarrays_with_equal_sum.rb) |
| 2399. Check Distances Between Same Letters | [Link](https://leetcode.com/problems/check-distances-between-same-letters/) | [Link](./lib/easy/2399_check_distances_between_same_letters.rb) |
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 = '5.6.5'
s.version = '5.6.6'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
28 changes: 28 additions & 0 deletions lib/easy/2399_check_distances_between_same_letters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# https://leetcode.com/problems/check-distances-between-same-letters/
# @param {String} s
# @param {Integer[]} distance
# @return {Boolean}
def check_distances(s, distance)
letters = ::Array.new(26, -1)
(0...s.length).each do |i|
c = s[i].ord - 97

letters[c] =
if letters[c] == -1
i
else
i - letters[c] - 1
end
end

(0...distance.length).each do |i|
dist = distance[i]
let = letters[i]

return false if let != -1 && let != dist
end

true
end
22 changes: 22 additions & 0 deletions test/easy/test_2399_check_distances_between_same_letters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/2399_check_distances_between_same_letters'
require 'minitest/autorun'

class CheckDistancesBetweenSameLettersTest < ::Minitest::Test
def test_default
assert(
check_distances(
'abaccb',
[1, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
)
)
assert(
!check_distances(
'aa',
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
)
)
end
end
Loading