Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 19 additions & 0 deletions lib/medium/678_valid_parenthesis_string.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/medium/test_678_valid_parenthesis_string.rb
Original file line number Diff line number Diff line change
@@ -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
Loading