File tree Expand file tree Collapse file tree 4 files changed +55
-1
lines changed Expand file tree Collapse file tree 4 files changed +55
-1
lines changed Original file line number Diff line number Diff 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 ) |
Original file line number Diff line number Diff 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'
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments