-
Notifications
You must be signed in to change notification settings - Fork 1
/
longest_palindrome.rb
51 lines (39 loc) · 1.3 KB
/
longest_palindrome.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'pry'
require 'pp'
def get_panlindrome(string, start, tail)
while string[start] == string[tail] && start >= 0 && tail < string.length
start -= 1
tail += 1
end
string[start + 1..tail - 1]
end
# Running time is O(n^2)
# sudo code:
# 1. return string if the length is one.
# 2. initialize the longest palindrome as the first letter.
# 3. iterate each letter from the beginning.
# case 1) treat each letter as the pivot, we want to compare letter both from left and right.
# case 2) treat each letter and the following as the povit, we want to compare letter both from left and right.
# keep moving the pointers until value of letter does not match
# 4. replace the palindrome with the longest returning value.
def longest_palindrome(string)
length = string.length
return string if length == 1
longest = string[0]
for i in 0..length - 1
palindrome = get_panlindrome(string, i, i)
if palindrome.length > longest.length
longest = palindrome
end
palindrome = get_panlindrome(string, i, i+1)
if palindrome.length > longest.length
longest = palindrome
end
end
longest
end
pp longest_palindrome('aba')
pp longest_palindrome('abadd')
pp longest_palindrome('abba')
pp longest_palindrome('sadfjhdsakjfabba')
pp longest_palindrome('ddddffddddabba')