diff --git a/README.md b/README.md index 27882fd4..ed466695 100644 --- a/README.md +++ b/README.md @@ -670,6 +670,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | | 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 abe3316b..d9cab579 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.7' + s.version = '8.3.8' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1249_minimum_remove_to_make_valid_parentheses.rb b/lib/medium/1249_minimum_remove_to_make_valid_parentheses.rb new file mode 100644 index 00000000..517051d9 --- /dev/null +++ b/lib/medium/1249_minimum_remove_to_make_valid_parentheses.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'set' + +# https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/ +# @param {String} s +# @return {String} +def min_remove_to_make_valid(s) + stack = [] + + (0...s.size).each do |i| + curr = s[i] + + next if curr.match?(/[[:alpha:]]/) + + if curr == '(' + stack.push(i) + elsif !stack.empty? && s[stack.last] == '(' + stack.pop + else + stack.push(i) + end + end + + result = '' + set = stack.to_set + + (0...s.size).each { |i| result += s[i] unless set.include?(i) } + + result +end diff --git a/test/medium/test_1249_minimum_remove_to_make_valid_parentheses.rb b/test/medium/test_1249_minimum_remove_to_make_valid_parentheses.rb new file mode 100644 index 00000000..0c08c559 --- /dev/null +++ b/test/medium/test_1249_minimum_remove_to_make_valid_parentheses.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1249_minimum_remove_to_make_valid_parentheses' +require 'minitest/autorun' + +class MinimumRemoveToMakeValidParenthesesTest < ::Minitest::Test + def test_default_one + assert_equal( + 'lee(t(c)o)de', + min_remove_to_make_valid( + 'lee(t(c)o)de' + ) + ) + end + + def test_default_two + assert_equal( + 'ab(c)d', + min_remove_to_make_valid( + 'a)b(c)d' + ) + ) + end + + def test_default_three + assert_equal( + '', + min_remove_to_make_valid( + '))((' + ) + ) + end +end