0

Recently, I have been working on an algorithm to find the longest palindrome in a string (ignoring spaces & case-insensitive). I have been using Ruby, and I have developed this windowing search algorithm .

My question is: Is there a faster way to find the longest palindrome in a string (on a non-specific case basis)?

def longest_palindrome(input)
  string = input.gsub(" ","")                # remove white spaces                     
  string.downcase!                           # convert to lowercase  
  length = string.length
  search = length - 1                        # search window size
  while search > 0                           # search while chars > 2
    for i in 0..(length-search)              # search every window 
      sub = string[i..(search+i)]            # check substring
      return sub if is_palindrome?(sub)
    end                                      # return asap once found
    search -= 1                              # dec. window size by 1
  end
  return false                               # false if none found
end

def is_palindrome?(string)
  return false if string.length < 2          # false if 1 character
  array = string.chars
  n = array.length-1                         # last string index
  for i in 0..(n/2)                          # check if whole string
    return false if array[i] != array[n-i]   # check if mirror image          
  end
  return true                                # true if all tests pass
end
Asleepace
  • 3,466
  • 2
  • 23
  • 36
  • Possible duplicate of [Write a function that returns the longest palindrome in a given string](http://stackoverflow.com/questions/1115001/write-a-function-that-returns-the-longest-palindrome-in-a-given-string) – Mihriban Minaz Apr 08 '16 at 10:06
  • 4
    You should probably ask this on http://codereview.stackexchange.com – Stefan Apr 08 '16 at 10:25
  • My function is theoretically faster than the one you posted Mihriban Minaz, becuase in theirs they start off at the smallest possible size and replace if they find a bigger one (which in some cases is faster), however mine goes backwards and starts off at the maximum possible size (i.e. it short-circuits out once found).... Also I've been wondering what is the difference between Stack Overflow and Stack Exchange? – Asleepace Apr 08 '16 at 10:57
  • I guess this is more of a theoretical answer than a technical one, however there has been much debate in my class since I wrote this, so I want to put things to rest one way or another. (I'm usually wrong anyways) – Asleepace Apr 08 '16 at 11:06
  • Welcome to SO! Please don't just link to an external resource containing your code - [edit] your question and add the code. Apart from that, as @Stefan already mentioned, this question isn't a very good fit for SO. – Frank Schmitt Apr 08 '16 at 11:08
  • Ok thank you for telling me that, I will be sure to edit now! – Asleepace Apr 08 '16 at 11:19
  • This question is also addressed [here](http://stackoverflow.com/questions/35570149/longest-palindrome-within-a-string). Both answers there start with the largest possible palindrome (the entire string) and progressively examine shorter substrings until a palindrom is found. (I especially like the second answer at that link.) In general, when code is presented, SO is the proper venue it's broken, CR when it works. In fact, CR requires working code. – Cary Swoveland Apr 08 '16 at 16:48

0 Answers0