Skip to content

Commit dc673cc

Browse files
committed
2024-05-13 v. 5.6.6: added "2399. Check Distances Between Same Letters"
1 parent fa51afc commit dc673cc

4 files changed

+52
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
426426
| 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) |
427427
| 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) |
428428
| 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) |
429+
| 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) |

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 = '5.6.5'
8+
s.version = '5.6.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/check-distances-between-same-letters/
4+
# @param {String} s
5+
# @param {Integer[]} distance
6+
# @return {Boolean}
7+
def check_distances(s, distance)
8+
letters = ::Array.new(26, -1)
9+
(0...s.length).each do |i|
10+
c = s[i].ord - 97
11+
12+
letters[c] =
13+
if letters[c] == -1
14+
i
15+
else
16+
i - letters[c] - 1
17+
end
18+
end
19+
20+
(0...distance.length).each do |i|
21+
dist = distance[i]
22+
let = letters[i]
23+
24+
return false if let != -1 && let != dist
25+
end
26+
27+
true
28+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2399_check_distances_between_same_letters'
5+
require 'minitest/autorun'
6+
7+
class CheckDistancesBetweenSameLettersTest < ::Minitest::Test
8+
def test_default
9+
assert(
10+
check_distances(
11+
'abaccb',
12+
[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]
13+
)
14+
)
15+
assert(
16+
!check_distances(
17+
'aa',
18+
[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]
19+
)
20+
)
21+
end
22+
end

0 commit comments

Comments
 (0)