Skip to content

Instantly share code, notes, and snippets.

@chuckg
Created September 29, 2011 20:34
Show Gist options
  • Save chuckg/1251851 to your computer and use it in GitHub Desktop.
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)).
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