Skip to content

Commit d6b7d8c

Browse files
authored
2025-01-06 v. 7.7.3: added "115. Distinct Subsequences"
2 parents 93b389a + 738077c commit d6b7d8c

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
642642
| 30. Substring with Concatenation of All Words | [Link](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [Link](./lib/hard/30_substring_with_concatenation_of_all_words.rb) | [Link](./test/hard/test_30_substring_with_concatenation_of_all_words.rb) |
643643
| 32. Longest Valid Parentheses | [Link](https://leetcode.com/problems/longest-valid-parentheses/) | [Link](./lib/hard/32_longest_valid_parentheses.rb) | [Link](./test/hard/test_32_longest_valid_parentheses.rb) |
644644
| 41. First Missing Positive | [Link](https://leetcode.com/problems/first-missing-positive/) | [Link](./lib/hard/41_first_missing_positive.rb) | [Link](./test/hard/test_41_first_missing_positive.rb) |
645+
| 115. Distinct Subsequences | [Link](https://leetcode.com/problems/distinct-subsequences/) | [Link](./lib/hard/115_distinct_subsequences.rb) | [Link](./test/hard/test_115_distinct_subsequences.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 = '7.7.2'
8+
s.version = '7.7.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/distinct-subsequences/
4+
# @param {String} s
5+
# @param {String} t
6+
# @return {Integer}
7+
def num_distinct(s, t)
8+
m = s.size
9+
n = t.size
10+
dp = ::Array.new(n + 1, 0)
11+
dp[0] = 1
12+
13+
(1..m).each do |i|
14+
(1..n).reverse_each do |j|
15+
dp[j] = dp[j - 1] + dp[j] if s[i - 1] == t[j - 1]
16+
end
17+
end
18+
19+
dp[n]
20+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/115_distinct_subsequences'
5+
require 'minitest/autorun'
6+
7+
class DistinctSubsequencesTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
3,
11+
num_distinct(
12+
'rabbbit',
13+
'rabbit'
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
5,
21+
num_distinct(
22+
'babgbag',
23+
'bag'
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)