Skip to content

Commit 244e115

Browse files
committed
2024-12-17 v. 7.3.6: added "647. Palindromic Substrings"
1 parent 296e91b commit 244e115

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
600600
| 622. Design Circular Queue | [Link](https://leetcode.com/problems/design-circular-queue/) | [Link](./lib/medium/622_design_circular_queue.rb) | [Link](./test/medium/test_622_design_circular_queue.rb) |
601601
| 623. Add One Row to Tree | [Link](https://leetcode.com/problems/add-one-row-to-tree/) | [Link](./lib/medium/623_add_one_row_to_tree.rb) | [Link](./test/medium/test_623_add_one_row_to_tree.rb) |
602602
| 641. Design Circular Deque | [Link](https://leetcode.com/problems/design-circular-deque/) | [Link](./lib/medium/641_design_circular_deque.rb) | [Link](./test/medium/test_641_design_circular_deque.rb) |
603+
| 647. Palindromic Substrings | [Link](https://leetcode.com/problems/palindromic-substrings/) | [Link](./lib/medium/647_palindromic_substrings.rb) | [Link](./test/medium/test_647_palindromic_substrings.rb) |

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.3.5'
8+
s.version = '7.3.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/palindromic-substrings/
4+
# @param {String} s
5+
# @return {Integer}
6+
def count_substrings(s)
7+
count = 0
8+
s.size.times do |i|
9+
count += palindrome_count_for_string(s, i, i)
10+
count += palindrome_count_for_string(s, i, i + 1)
11+
end
12+
13+
count
14+
end
15+
16+
private
17+
18+
# @param {String} s
19+
# @param {Integer} left
20+
# @param {Integer} right
21+
# @return {Integer}
22+
def palindrome_count_for_string(s, left, right)
23+
count = 0
24+
while left >= 0 && right < s.length && s[left] == s[right]
25+
count += 1
26+
left -= 1
27+
right += 1
28+
end
29+
30+
count
31+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/647_palindromic_substrings'
5+
require 'minitest/autorun'
6+
7+
class PalindromicSubstringsTest < ::Minitest::Test
8+
def test_default_one = assert_equal(3, count_substrings('abc'))
9+
10+
def test_default_two = assert_equal(6, count_substrings('aaa'))
11+
end

0 commit comments

Comments
 (0)