From b152a87dc36f68185379498213e7a159482e4002 Mon Sep 17 00:00:00 2001 From: fartem Date: Sun, 12 Jan 2025 09:19:57 +0300 Subject: [PATCH] 2025-01-12 v. 7.9.3: added "2116. Check if a Parentheses String Can Be Valid" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...ck_if_a_parentheses_string_can_be_valid.rb | 26 +++++++++++++++++++ ...ck_if_a_parentheses_string_can_be_valid.rb | 13 ++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb create mode 100644 test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb diff --git a/README.md b/README.md index baa2ad3c..020d2023 100644 --- a/README.md +++ b/README.md @@ -641,6 +641,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 912. Sort an Array | [Link](https://leetcode.com/problems/sort-an-array/) | [Link](./lib/medium/912_sort_an_array.rb) | [Link](./test/medium/test_912_sort_an_array.rb) | | 916. Word Subsets | [Link](https://leetcode.com/problems/word-subsets/) | [Link](./lib/medium/916_word_subsets.rb) | [Link](./test/medium/test_916_word_subsets.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) | ### Hard diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 5b1711e4..bfcb75c1 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 = '7.9.2' + s.version = '7.9.3' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb b/lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb new file mode 100644 index 00000000..a805c09e --- /dev/null +++ b/lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/ +# @param {String} s +# @param {String} locked +# @return {Boolean} +def can_be_valid(s, locked) + return false if s.size.odd? + + min = 0 + max = 0 + + (0...s.size).each do |i| + is_opening = s[i] == '(' + is_unlocked = locked[i] == '0' + + min += !is_opening || is_unlocked ? -1 : 1 + max += is_opening || is_unlocked ? 1 : -1 + + return false if max.negative? + + min = [min, 0].max + end + + min.zero? +end diff --git a/test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb b/test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb new file mode 100644 index 00000000..e85b6200 --- /dev/null +++ b/test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/2116_check_if_a_parentheses_string_can_be_valid' +require 'minitest/autorun' + +class CheckIfAParenthesesStringCanBeValidTest < ::Minitest::Test + def test_default_one = assert(can_be_valid('))()))', '010100')) + + def test_default_two = assert(can_be_valid('()()', '0000')) + + def test_default_three = assert(!can_be_valid(')', '0')) +end