Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '8.2.8'
s.version = '8.2.9'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
23 changes: 23 additions & 0 deletions lib/medium/1081_smallest_subsequence_of_distinct_characters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
# @param {String} s
# @return {String}
def smallest_subsequence(s)
last_indices = {}
s.each_char.with_index { |c, i| last_indices[c] = i }

seen = ::Set.new
stack = []

s.each_char.with_index do |c, i|
next if seen.include?(c)

seen.delete(stack.pop) while !stack.empty? && c < stack.last && i < last_indices[stack.last]

stack << c
seen << c
end

stack.join
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/1081_smallest_subsequence_of_distinct_characters'
require 'minitest/autorun'

class SmallestSubsequenceOfDistinctCharactersTest < ::Minitest::Test
def test_default_one
assert_equal(
'abc',
smallest_subsequence('bcabc')
)
end

def test_default_two
assert_equal(
'acdb',
smallest_subsequence('cbacdcbc')
)
end
end
Loading