-
-
Notifications
You must be signed in to change notification settings - Fork 596
Add Longest Palindromic Substring in Ruby #4051
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a02bf6c
Creating the function and spliting the input
BahramT1 6599f77
Starting index with and then going through each word
BahramT1 514b8f7
Check if the program has received input from the command line
BahramT1 daa8978
Creating Function
BahramT1 92f61ce
Looping through each Character in the string and checking the longest…
BahramT1 958476f
Updated the readme file
BahramT1 b8dd02b
Completed the Longest Palindromic Substring file
BahramT1 aecdef1
Merge branch 'main' into main
rzuckerm 967727d
Merge branch 'main' into main
rzuckerm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
def longest_palindromic_substring(s) | ||
return "Usage: please provide a string that contains at least one palindrome" if s.nil? || s.empty? | ||
|
||
longest = "" | ||
|
||
# Loop through each character in the string | ||
(0...s.length).each do |i| | ||
# Check for the longest odd-length palindrome centered at `i` | ||
odd_palindrome = expand_from_center(s, i, i) | ||
# Check for the longest even-length palindrome centered between `i` and `i+1` | ||
even_palindrome = expand_from_center(s, i, i + 1) | ||
|
||
# Update longest if we found a longer palindrome | ||
longest = [longest, odd_palindrome, even_palindrome].max_by(&:length) | ||
end | ||
|
||
longest | ||
end | ||
|
||
# Helper function to expand from the center and find a palindrome | ||
def expand_from_center(s, left, right) | ||
# Expand outwards while the characters are the same | ||
while left >= 0 && right < s.length && s[left] == s[right] | ||
left -= 1 | ||
right += 1 | ||
end | ||
# Return the substring that is a palindrome | ||
s[(left + 1)...right] | ||
end | ||
|
||
# Example usage | ||
puts longest_palindromic_substring("paapaapap") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
def longest_word_length(input) | ||
|
||
# Split the input by spaces | ||
words = input.split(" ") | ||
|
||
# Start with 0 as the longest length | ||
max_length = 0 | ||
|
||
# Go through each word | ||
words.each do |word| | ||
if word.length > max_length | ||
max_length = word.length | ||
end | ||
end | ||
|
||
# Return the length of the longest word | ||
max_length | ||
end | ||
|
||
# Check if the program has received input from the command line | ||
if ARGV.length > 0 | ||
input_string = ARGV.join(" ") | ||
puts longest_word_length(input_string) | ||
else | ||
puts "Please provide an input string." | ||
end | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is supposed to accept command-line arguments. See the project description for details. In general, all of the projects are list here. Just click on the link for the project you are interested in.