Created
September 29, 2011 20:34
-
-
Save chuckg/1251851 to your computer and use it in GitHub Desktop.
Find the largest palindrome in a string the slow way (not O(n)).
This file contains 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
sentence = "i am a big radar and i like to play aaa with toys".split.join | |
largest = '' | |
for i in 0..(sentence.size - 1) | |
first_char = sentence[0..0] | |
sentence = sentence[1..sentence.size] | |
segment = '' | |
for j in 0..(sentence.size - 1) | |
segment << sentence[j] | |
test = first_char + segment | |
if test == test.reverse && test.size > largest.size | |
largest = test | |
end | |
end | |
end | |
puts largest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment