Advertisement
naimul64

Largest Palindrome Leetcode

Oct 29th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. class Solution(object):
  2.     def isFirstLarger(dic1, dic2, start, end):
  3.         if dic1 is None:
  4.             return False
  5.         if dic2 is None:
  6.             return True
  7.  
  8.         lenfirst = dic1[end] - dic1[start]
  9.         lensecond = dic2[end] - dic2[start]
  10.  
  11.         return lenfirst > lensecond
  12.  
  13.     def recursivePalindomeFinder(string, dictionary, largest, largestLen, start, end):
  14.         if dictionary[start] == 0 or dictionary[end] == length:
  15.             return
  16.         if string[dictionary[start] - 1] == string[dictionary[end] + 1]:
  17.             if dictionary[end] - dictionary[start] + 3 > largestLen:
  18.                 largestLen = dictionary[end] - dictionary[start] + 3
  19.                 largest = {start: dictionary[start] - 1, end: dictionary[end] + 1}
  20.         else:
  21.             return
  22.  
  23.     def longestPalindrome(self, s):
  24.         start = 'start'
  25.         end = 'end'
  26.         largest = None
  27.         largestLen = 0
  28.  
  29.         print 'ok upto this'
  30.  
  31.         for i in range(len(s)):
  32.             curStr = s[i]
  33.             dic = {start: i, end: i}
  34.             if self.isFirstLarger(dic, largest, start, end):
  35.                 largest = dic
  36.                 largestLen = dic[end] - dic[start] + 1
  37.                 self.recursivePalindomeFinder(dic, largest, largestLen, start, end)
  38.  
  39.  
  40.  
  41.  
  42. sol = Solution()
  43. sol.longestPalindrome('101')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement