Skip to content

Commit

Permalink
2023-01-28 v. 1.3.2: added "680. Valid Palindrome II"
Browse files Browse the repository at this point in the history
  • Loading branch information
fartem committed Jul 28, 2023
1 parent fe125d3 commit 2b19e2d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 657. Robot Return to Origin | [Link](https://leetcode.com/problems/robot-return-to-origin/) | [Link](./lib/easy/657_robot_return_to_origin.rb) |
| 671. Second Minimum Node In a Binary Tree | [Link](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [Link](./lib/easy/671_second_minimum_node_in_a_binary_tree.rb) |
| 674. Longest Continuous Increasing Subsequence | [Link](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Link](./lib/easy/674_longest_continuous_increasing_subsequence.rb) |
| 680. Valid Palindrome II | [Link](https://leetcode.com/problems/valid-palindrome-ii/) | [Link](./lib/easy/680_valid_palindrome_ii.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 = '1.3.1'
s.version = '1.3.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
24 changes: 24 additions & 0 deletions lib/easy/680_valid_palindrome_ii.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# https://leetcode.com/problems/valid-palindrome-ii/
# @param {String} s
# @return {Boolean}
def valid_palindrome_ii(s)
(0...(s.length / 2)).step(1) do |i|
index = s.length - i - 1
return is_palindrome_range(s, i + 1, index) || is_palindrome_range(s, i, index - 1) if s[i] != s[index]
end

true
end

# @param {String} s
# @param {Integer} i
# @param {Integer} j
def is_palindrome_range(s, i, j)
(i..(i + (j - i) / 2)).step(1) do |k|
return false if s[k] != s[j - k + i]
end

true
end
13 changes: 13 additions & 0 deletions test/easy/test_680_valid_palindrome.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/easy/680_valid_palindrome_ii'
require 'minitest/autorun'

class ValidPalindromeIITest < ::Minitest::Test
def test_default
assert(valid_palindrome_ii('aba'))
assert(valid_palindrome_ii('abca'))
assert(!valid_palindrome_ii('abc'))
end
end

0 comments on commit 2b19e2d

Please sign in to comment.