Skip to content

Commit 5f52a64

Browse files
authored
2025-02-11 v. 8.5.1: added "1358. Number of Substrings Containing All Three Characters"
2 parents d67655b + 2497c76 commit 5f52a64

4 files changed

+39
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
683683
| 1339. Maximum Product of Splitted Binary Tree | [Link](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Link](./lib/medium/1339_maximum_product_of_splitted_binary_tree.rb) | [Link](./test/medium/test_1339_maximum_product_of_splitted_binary_tree.rb) |
684684
| 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold | [Link](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) | [Link](./lib/medium/1343_number_of_sub_arrays_of_size_k_and_average_greater_than_or_equal_to_threshold.rb) | [Link](./test/medium/test_1343_number_of_sub_arrays_of_size_k_and_average_greater_than_or_equal_to_threshold.rb) |
685685
| 1347. Minimum Number of Steps to Make Two Strings Anagram | [Link](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | [Link](./lib/medium/1347_minimum_number_of_steps_to_make_two_strings_anagram.rb) | [Link](./test/medium/test_1347_minimum_number_of_steps_to_make_two_strings_anagram.rb) |
686+
| 1358. Number of Substrings Containing All Three Characters | [Link](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/) | [Link](./lib/medium/1358_number_of_substrings_containing_all_three_characters.rb) | [Link](./test/medium/test_1358_number_of_substrings_containing_all_three_characters.rb) |
686687
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
687688
| 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) |
688689
| 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) |

leetcode-ruby.gemspec

+1-1
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.5.0'
8+
s.version = '8.5.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/
4+
# @param {String} s
5+
# @return {Integer}
6+
def number_of_substrings(s)
7+
chars = ::Array.new(3, 0)
8+
left = 0
9+
result = 0
10+
11+
(0...s.size).each do |i|
12+
chars[s[i].ord - 97] += 1
13+
14+
while chars[0].positive? && chars[1].positive? && chars[2].positive?
15+
result += s.size - i
16+
17+
chars[s[left].ord - 97] -= 1
18+
19+
left += 1
20+
end
21+
end
22+
23+
result
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1358_number_of_substrings_containing_all_three_characters'
5+
require 'minitest/autorun'
6+
7+
class NumberOfSubstringsContainingAllThreeCharactersTest < ::Minitest::Test
8+
def test_default_one = assert_equal(10, number_of_substrings('abcabc'))
9+
10+
def test_default_two = assert_equal(3, number_of_substrings('aaacb'))
11+
12+
def test_default_three = assert_equal(1, number_of_substrings('abc'))
13+
end

0 commit comments

Comments
 (0)