diff --git a/README.md b/README.md index 459145ff..7a1114da 100644 --- a/README.md +++ b/README.md @@ -666,6 +666,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1111. Maximum Nesting Depth of Two Valid Parentheses Strings | [Link](https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/) | [Link](./lib/medium/1111_maximum_nesting_depth_of_two_valid_parentheses_strings.rb) | [Link](./test/medium/test_1111_maximum_nesting_depth_of_two_valid_parentheses_strings.rb) | | 1123. Lowest Common Ancestor of Deepest Leaves | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/) | [Link](./lib/medium/1123_lowest_common_ancestor_of_deepest_leaves.rb) | [Link](./test/medium/test_1123_lowest_common_ancestor_of_deepest_leaves.rb) | | 1161. Maximum Level Sum of a Binary Tree | [Link](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) | [Link](./lib/medium/1161_maximum_level_sum_of_a_binary_tree.rb) | [Link](./test/medium/test_1161_maximum_level_sum_of_a_binary_tree.rb) | +| 1209. Remove All Adjacent Duplicates in String II | [Link](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/) | [Link](./lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii.rb) | [Link](./test/medium/test_1209_remove_all_adjacent_duplicates_in_string_ii.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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index a561b987..5f87c9d9 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 = '8.3.3' + s.version = '8.3.4' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii.rb b/lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii.rb new file mode 100644 index 00000000..167ed3b0 --- /dev/null +++ b/lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/ +# @param {String} s +# @param {Integer} k +# @return {String} +def remove_duplicates_adjacent(s, k) + stack = [] + + s.each_char do |c| + if !stack.empty? && c == stack.last[0] + if stack.last[1] == k - 1 + stack.pop + else + stack.last[1] += 1 + end + else + stack << [c, 1] + end + end + + stack.map { |char, count| char * count }.join +end diff --git a/test/medium/test_1209_remove_all_adjacent_duplicates_in_string_ii.rb b/test/medium/test_1209_remove_all_adjacent_duplicates_in_string_ii.rb new file mode 100644 index 00000000..6ec5cc6e --- /dev/null +++ b/test/medium/test_1209_remove_all_adjacent_duplicates_in_string_ii.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii' +require 'minitest/autorun' + +class RemoveAllAdjacentDuplicatesInStringIITest < ::Minitest::Test + def test_default_one + assert_equal( + 'abcd', + remove_duplicates_adjacent( + 'abcd', + 2 + ) + ) + end + + def test_default_two + assert_equal( + 'aa', + remove_duplicates_adjacent( + 'deeedbbcccbdaa', + 3 + ) + ) + end + + def test_default_three + assert_equal( + 'ps', + remove_duplicates_adjacent( + 'pbbcggttciiippooaais', + 2 + ) + ) + end +end