Skip to content

Commit b152a87

Browse files
committed
2025-01-12 v. 7.9.3: added "2116. Check if a Parentheses String Can Be Valid"
1 parent 96c786f commit b152a87

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
641641
| 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) |
642642
| 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) |
643643
| 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) |
644+
| 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) |
644645

645646
### Hard
646647

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
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 = '7.9.2'
8+
s.version = '7.9.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/
4+
# @param {String} s
5+
# @param {String} locked
6+
# @return {Boolean}
7+
def can_be_valid(s, locked)
8+
return false if s.size.odd?
9+
10+
min = 0
11+
max = 0
12+
13+
(0...s.size).each do |i|
14+
is_opening = s[i] == '('
15+
is_unlocked = locked[i] == '0'
16+
17+
min += !is_opening || is_unlocked ? -1 : 1
18+
max += is_opening || is_unlocked ? 1 : -1
19+
20+
return false if max.negative?
21+
22+
min = [min, 0].max
23+
end
24+
25+
min.zero?
26+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/2116_check_if_a_parentheses_string_can_be_valid'
5+
require 'minitest/autorun'
6+
7+
class CheckIfAParenthesesStringCanBeValidTest < ::Minitest::Test
8+
def test_default_one = assert(can_be_valid('))()))', '010100'))
9+
10+
def test_default_two = assert(can_be_valid('()()', '0000'))
11+
12+
def test_default_three = assert(!can_be_valid(')', '0'))
13+
end

0 commit comments

Comments
 (0)