Skip to content

2024-06-25 v. 5.9.6: added "5. Longest Palindromic Substring" #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2024
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 @@ -461,3 +461,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
|---------------------------------------------------|---------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
| 2. Add Two Numbers | [Link](https://leetcode.com/problems/add-two-numbers/) | [Link](./lib/medium/2_add_two_numbers.rb) |
| 3. Longest Substring Without Repeating Characters | [Link](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Link](./lib/medium/3_longest_substring_without_repeating_characters.rb) |
| 5. Longest Palindromic Substring | [Link](https://leetcode.com/problems/longest-palindromic-substring/) | [Link](./lib/medium/5_longest_palindromic_substring.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 = '5.9.5'
s.version = '5.9.6'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
30 changes: 30 additions & 0 deletions lib/medium/5_longest_palindromic_substring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# https://leetcode.com/problems/longest-palindromic-substring/
# @param {String} s
# @return {String}
def longest_palindrome5(s)
result = ''

(0...s.length).each do |i|
str1 = find_longest_palindrome(i, i, s)
str2 = find_longest_palindrome(i, i + 1, s)

result = [result, str1, str2].max_by(&:size)
end

result
end

# @param {Integer} l
# @param {Integer} r
# @param {String} s
# @return {String}
def find_longest_palindrome(l, r, s)
while l >= 0 && r < s.size && s[l] == s[r]
l -= 1
r += 1
end

s[(l + 1)..(r - 1)]
end
12 changes: 12 additions & 0 deletions test/medium/test_5_longest_palindromic_substring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/5_longest_palindromic_substring'
require 'minitest/autorun'

class LongestPalindromicSubstringTest < ::Minitest::Test
def test_default
assert_equal('bab', longest_palindrome5('babad'))
assert_equal('bb', longest_palindrome5('cbbd'))
end
end
Loading