From 738077c44bc77cd35628e6b96a0d2d6153997406 Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 6 Jan 2025 14:31:00 +0300 Subject: [PATCH] 2025-01-06 v. 7.7.3: added "115. Distinct Subsequences" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/hard/115_distinct_subsequences.rb | 20 +++++++++++++++ test/hard/test_115_distinct_subsequences.rb | 27 +++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lib/hard/115_distinct_subsequences.rb create mode 100644 test/hard/test_115_distinct_subsequences.rb diff --git a/README.md b/README.md index fa05504e..f08882c2 100644 --- a/README.md +++ b/README.md @@ -642,3 +642,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index bc4dcf37..fee7fb61 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '7.7.2' + s.version = '7.7.3' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/hard/115_distinct_subsequences.rb b/lib/hard/115_distinct_subsequences.rb new file mode 100644 index 00000000..366833ad --- /dev/null +++ b/lib/hard/115_distinct_subsequences.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/distinct-subsequences/ +# @param {String} s +# @param {String} t +# @return {Integer} +def num_distinct(s, t) + m = s.size + n = t.size + dp = ::Array.new(n + 1, 0) + dp[0] = 1 + + (1..m).each do |i| + (1..n).reverse_each do |j| + dp[j] = dp[j - 1] + dp[j] if s[i - 1] == t[j - 1] + end + end + + dp[n] +end diff --git a/test/hard/test_115_distinct_subsequences.rb b/test/hard/test_115_distinct_subsequences.rb new file mode 100644 index 00000000..9f5404bc --- /dev/null +++ b/test/hard/test_115_distinct_subsequences.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/hard/115_distinct_subsequences' +require 'minitest/autorun' + +class DistinctSubsequencesTest < ::Minitest::Test + def test_default_one + assert_equal( + 3, + num_distinct( + 'rabbbit', + 'rabbit' + ) + ) + end + + def test_default_two + assert_equal( + 5, + num_distinct( + 'babgbag', + 'bag' + ) + ) + end +end