Skip to content

Commit d100e3b

Browse files
authored
2025-01-27 v. 8.2.9: added "1081. Smallest Subsequence of Distinct Characters"
2 parents bd8af17 + 617f3e3 commit d100e3b

4 files changed

+46
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
661661
| 1038. Binary Search Tree to Greater Sum Tree | [Link](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/) | [Link](./lib/medium/1038_binary_search_tree_to_greater_sum_tree.rb) | [Link](./test/medium/test_1038_binary_search_tree_to_greater_sum_tree.rb) |
662662
| 1043. Partition Array for Maximum Sum | [Link](https://leetcode.com/problems/partition-array-for-maximum-sum/) | [Link](./lib/medium/1043_partition_array_for_maximum_sum.rb) | [Link](./test/medium/test_1043_partition_array_for_maximum_sum.rb) |
663663
| 1079. Letter Tile Possibilities | [Link](https://leetcode.com/problems/letter-tile-possibilities/) | [Link](./lib/medium/1079_letter_tile_possibilities.rb) | [Link](./test/medium/test_1079_letter_tile_possibilities.rb) |
664+
| 1081. Smallest Subsequence of Distinct Characters | [Link](https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/) | [Link](./lib/medium/1081_smallest_subsequence_of_distinct_characters.rb) | [Link](./test/medium/test_1081_smallest_subsequence_of_distinct_characters.rb) |
664665
| 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) |
665666
| 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) |
666667
| 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.2.8'
8+
s.version = '8.2.9'
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,23 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
4+
# @param {String} s
5+
# @return {String}
6+
def smallest_subsequence(s)
7+
last_indices = {}
8+
s.each_char.with_index { |c, i| last_indices[c] = i }
9+
10+
seen = ::Set.new
11+
stack = []
12+
13+
s.each_char.with_index do |c, i|
14+
next if seen.include?(c)
15+
16+
seen.delete(stack.pop) while !stack.empty? && c < stack.last && i < last_indices[stack.last]
17+
18+
stack << c
19+
seen << c
20+
end
21+
22+
stack.join
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1081_smallest_subsequence_of_distinct_characters'
5+
require 'minitest/autorun'
6+
7+
class SmallestSubsequenceOfDistinctCharactersTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'abc',
11+
smallest_subsequence('bcabc')
12+
)
13+
end
14+
15+
def test_default_two
16+
assert_equal(
17+
'acdb',
18+
smallest_subsequence('cbacdcbc')
19+
)
20+
end
21+
end

0 commit comments

Comments
 (0)