Skip to content

Commit 4db4dcb

Browse files
authored
2025-02-03 v. 8.3.8: added "1249. Minimum Remove to Make Valid Parentheses"
2 parents 164405a + c7cf104 commit 4db4dcb

4 files changed

+67
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
670670
| 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) |
671671
| 1247. Minimum Swaps to Make Strings Equal | [Link](https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/) | [Link](./lib/medium/1247_minimum_swaps_to_make_strings_equal.rb) | [Link](./test/medium/test_1247_minimum_swaps_to_make_strings_equal.rb) |
672672
| 1248. Count Number of Nice Subarrays | [Link](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [Link](./lib/medium/1248_count_number_of_nice_subarrays.rb) | [Link](./test/medium/test_1248_count_number_of_nice_subarrays.rb) |
673+
| 1249. Minimum Remove to Make Valid Parentheses | [Link](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [Link](./lib/medium/1249_minimum_remove_to_make_valid_parentheses.rb) | [Link](./test/medium/test_1249_minimum_remove_to_make_valid_parentheses.rb) |
673674
| 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) |
674675
| 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) |
675676
| 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.7'
8+
s.version = '8.3.8'
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,31 @@
1+
# frozen_string_literal: true
2+
3+
require 'set'
4+
5+
# https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
6+
# @param {String} s
7+
# @return {String}
8+
def min_remove_to_make_valid(s)
9+
stack = []
10+
11+
(0...s.size).each do |i|
12+
curr = s[i]
13+
14+
next if curr.match?(/[[:alpha:]]/)
15+
16+
if curr == '('
17+
stack.push(i)
18+
elsif !stack.empty? && s[stack.last] == '('
19+
stack.pop
20+
else
21+
stack.push(i)
22+
end
23+
end
24+
25+
result = ''
26+
set = stack.to_set
27+
28+
(0...s.size).each { |i| result += s[i] unless set.include?(i) }
29+
30+
result
31+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1249_minimum_remove_to_make_valid_parentheses'
5+
require 'minitest/autorun'
6+
7+
class MinimumRemoveToMakeValidParenthesesTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
'lee(t(c)o)de',
11+
min_remove_to_make_valid(
12+
'lee(t(c)o)de'
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
'ab(c)d',
20+
min_remove_to_make_valid(
21+
'a)b(c)d'
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
'',
29+
min_remove_to_make_valid(
30+
'))(('
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)