Skip to content

Commit cc4b663

Browse files
authored
2025-01-30 v. 8.3.4: added "1209. Remove All Adjacent Duplicates in String II"
2 parents 73aaaac + dace4d8 commit cc4b663

4 files changed

+62
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
666666
| 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) |
667667
| 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) |
668668
| 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) |
669+
| 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) |
669670
| 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) |
670671
| 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) |
671672
| 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.3.3'
8+
s.version = '8.3.4'
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/remove-all-adjacent-duplicates-in-string-ii/
4+
# @param {String} s
5+
# @param {Integer} k
6+
# @return {String}
7+
def remove_duplicates_adjacent(s, k)
8+
stack = []
9+
10+
s.each_char do |c|
11+
if !stack.empty? && c == stack.last[0]
12+
if stack.last[1] == k - 1
13+
stack.pop
14+
else
15+
stack.last[1] += 1
16+
end
17+
else
18+
stack << [c, 1]
19+
end
20+
end
21+
22+
stack.map { |char, count| char * count }.join
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii'
5+
require 'minitest/autorun'
6+
7+
class RemoveAllAdjacentDuplicatesInStringIITest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'abcd',
11+
remove_duplicates_adjacent(
12+
'abcd',
13+
2
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
'aa',
21+
remove_duplicates_adjacent(
22+
'deeedbbcccbdaa',
23+
3
24+
)
25+
)
26+
end
27+
28+
def test_default_three
29+
assert_equal(
30+
'ps',
31+
remove_duplicates_adjacent(
32+
'pbbcggttciiippooaais',
33+
2
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)