Skip to content

Commit c3c8325

Browse files
committed
2024-10-18 v. 6.8.4: added "316. Remove Duplicate Letters"
1 parent d964054 commit c3c8325

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
549549
| 299. Bulls and Cows | [Link](https://leetcode.com/problems/bulls-and-cows/) | [Link](./lib/medium/299_bulls_and_cows.rb) | [Link](./test/medium/test_299_bulls_and_cows.rb) |
550550
| 300. Longest Increasing Subsequence | [Link](https://leetcode.com/problems/longest-increasing-subsequence/) | [Link](./lib/medium/300_longest_increasing_subsequence.rb) | [Link](./test/medium/test_300_longest_increasing_subsequence.rb) |
551551
| 304. Range Sum Query 2D - Immutable | [Link](https://leetcode.com/problems/range-sum-query-2d-immutable/) | [Link](./lib/medium/304_range_sum_query_2d_immutable.rb) | [Link](./test/medium/test_304_range_sum_query_2d_immutable.rb) |
552+
| 316. Remove Duplicate Letters | [Link](https://leetcode.com/problems/remove-duplicate-letters/) | [Link](./lib/medium/316_remove_duplicate_letters.rb) | [Link](./test/medium/test_316_remove_duplicate_letters.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 = '6.8.3'
8+
s.version = '6.8.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/remove-duplicate-letters/
4+
# @param {String} s
5+
# @return {String}
6+
def remove_duplicate_letters(s)
7+
last_indices = ::Array.new(26, 0)
8+
s.each_char.with_index { |c, i| last_indices[c.ord - 'a'.ord] = i }
9+
10+
seen = ::Array.new(26, false)
11+
stack = []
12+
13+
s.each_char.with_index do |c, i|
14+
c_index = c.ord - 'a'.ord
15+
16+
next if seen[c_index]
17+
18+
seen[stack.pop] = false while !stack.empty? && stack.last > c_index && i < last_indices[stack.last]
19+
20+
stack.push(c_index)
21+
seen[c_index] = true
22+
end
23+
24+
result = ''
25+
result += (stack.pop + 'a'.ord).chr until stack.empty?
26+
27+
result.reverse
28+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/316_remove_duplicate_letters'
5+
require 'minitest/autorun'
6+
7+
class RemoveDuplicateLettersTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'abc',
11+
remove_duplicate_letters(
12+
'bcabc'
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
'acdb',
20+
remove_duplicate_letters(
21+
'cbacdcbc'
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)