From 226f3b8fe17127bf5a1f445900a27a57f76cd0c5 Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 23 Dec 2024 09:25:26 +0300 Subject: [PATCH] 2024-12-23 v. 7.4.4: added "678. Valid Parenthesis String" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/678_valid_parenthesis_string.rb | 19 +++++++++++++++++++ .../test_678_valid_parenthesis_string.rb | 13 +++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 lib/medium/678_valid_parenthesis_string.rb create mode 100644 test/medium/test_678_valid_parenthesis_string.rb diff --git a/README.md b/README.md index bc024470..0f073d4d 100644 --- a/README.md +++ b/README.md @@ -608,3 +608,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 662. Maximum Width of Binary Tree | [Link](https://leetcode.com/problems/maximum-width-of-binary-tree/) | [Link](./lib/medium/662_maximum_width_of_binary_tree.rb) | [Link](./test/medium/test_662_maximum_width_of_binary_tree.rb) | | 669. Trim a Binary Search Tree | [Link](https://leetcode.com/problems/trim-a-binary-search-tree/) | [Link](./lib/medium/669_trim_a_binary_search_tree.rb) | [Link](./test/medium/test_669_trim_a_binary_search_tree.rb) | | 677. Map Sum Pairs | [Link](https://leetcode.com/problems/map-sum-pairs/) | [Link](./lib/medium/677_map_sum_pairs.rb) | [Link](./test/medium/test_677_map_sum_pairs.rb) | +| 678. Valid Parenthesis String | [Link](https://leetcode.com/problems/valid-parenthesis-string/) | [Link](./lib/medium/678_valid_parenthesis_string.rb) | [Link](./test/medium/test_678_valid_parenthesis_string.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 0de0af34..86004f0b 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.4.3' + s.version = '7.4.4' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/678_valid_parenthesis_string.rb b/lib/medium/678_valid_parenthesis_string.rb new file mode 100644 index 00000000..2f9198a3 --- /dev/null +++ b/lib/medium/678_valid_parenthesis_string.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/valid-parenthesis-string/ +# @param {String} s +# @return {Boolean} +def check_valid_string(s) + l = 0 + h = 0 + s.each_char do |c| + l += c == '(' ? 1 : -1 + h += c == ')' ? -1 : 1 + + break if h.negative? + + l = [l, 0].max + end + + l.zero? +end diff --git a/test/medium/test_678_valid_parenthesis_string.rb b/test/medium/test_678_valid_parenthesis_string.rb new file mode 100644 index 00000000..23462b31 --- /dev/null +++ b/test/medium/test_678_valid_parenthesis_string.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/678_valid_parenthesis_string' +require 'minitest/autorun' + +class ValidParenthesisStringTest < ::Minitest::Test + def test_default_one = assert(check_valid_string('()')) + + def test_default_two = assert(check_valid_string('(*)')) + + def test_default_three = assert(check_valid_string('(*))')) +end