Skip to content

Commit fffb73f

Browse files
committed
2025-02-24 v. 8.6.8: added "1525. Number of Good Ways to Split a String"
1 parent d1356a0 commit fffb73f

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
701701
| 1481. Least Number of Unique Integers after K Removals | [Link](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/) | [Link](./lib/medium/1481_least_number_of_unique_integers_after_k_removals.rb) | [Link](./test/medium/test_1481_least_number_of_unique_integers_after_k_removals.rb) |
702702
| 1492. The kth Factor of n | [Link](https://leetcode.com/problems/the-kth-factor-of-n/) | [Link](./lib/medium/1492_the_kth_factor_of_n.rb) | [Link](./test/medium/test_1492_the_kth_factor_of_n.rb) |
703703
| 1504. Count Submatrices With All Ones | [Link](https://leetcode.com/problems/count-submatrices-with-all-ones/) | [Link](./lib/medium/1504_count_submatrices_with_all_ones.rb) | [Link](./test/medium/test_1504_count_submatrices_with_all_ones.rb) |
704+
| 1525. Number of Good Ways to Split a String | [Link](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [Link](./lib/medium/1525_number_of_good_ways_to_split_a_string.rb) | [Link](./test/medium/test_1525_number_of_good_ways_to_split_a_string.rb) |
704705
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
705706
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
706707
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.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 = '8.6.7'
8+
s.version = '8.6.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/number-of-good-ways-to-split-a-string/
4+
# @param {String} s
5+
# @return {Integer}
6+
def num_splits(s)
7+
l_chars = ::Set.new
8+
r_chars = ::Set.new
9+
l_count = ::Array.new(s.size, 0)
10+
r_count = ::Array.new(s.size, 0)
11+
12+
s.length.times do |i|
13+
l_c = s[i]
14+
15+
l_chars.add(l_c) unless l_chars.include?(l_c)
16+
17+
l_count[i] = l_chars.size
18+
r_index = s.length - i - 1
19+
r_c = s[r_index]
20+
21+
r_chars.add(r_c) unless r_chars.include?(r_c)
22+
23+
r_count[r_index] = r_chars.size
24+
end
25+
26+
result = 0
27+
l_p = 0
28+
r_p = 1
29+
30+
while l_p != s.size && r_p != s.size
31+
result += 1 if l_count[l_p] == r_count[r_p]
32+
33+
l_p += 1
34+
r_p += 1
35+
end
36+
37+
result
38+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1525_number_of_good_ways_to_split_a_string'
5+
require 'minitest/autorun'
6+
7+
class NumberOfGoodWaysToSplitAStringTest < ::Minitest::Test
8+
def test_default_one = assert_equal(2, num_splits('aacaba'))
9+
10+
def test_default_two = assert_equal(1, num_splits('abcd'))
11+
end

0 commit comments

Comments
 (0)