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