
class Solution(object):
	def longestPalindrome(self, s):
		"""
		:type s: str
        :rtype: str
		"""
		pass


def main():
	s = 'babad'
	print(Solution().longestPalindrome(s))

	s = 'cbbd'
	print(Solution().longestPalindrome(s))


if __name__ == '__main__':
	main()


class SolutionNaive(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str

        Time limit exceeded.
        """
        n = len(s)
        max_len = 1
        max_i = 0
        max_j = 0
        
        for i in range(n):
            for j in range(i + 1, n):
                if s[i:(j + 1)] == s[i:(j + 1)][::-1]:
                    ij_len = j - i + 1
                    if ij_len > max_len:
                        max_len = ij_len
                        max_i = i
                        max_j = j
                else:
                    break
        return s[max_i:(max_j + 1)]


class SolutionDP(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        pass


def main():
    s = 'babad'    # Ans: bab.
    print(SolutionNaive().longestPalindrome(s))

    s = 'cbbd'     # Ans: bb.
    print(SolutionNaive().longestPalindrome(s))


if __name__ == '__main__':
    main()

Complete naive longest palindromic substring
Complete naive longest palindromic substring

  
- class Solution(object):
+ class SolutionNaive(object):
- 	def longestPalindrome(self, s):
+     def longestPalindrome(self, s):
- 		"""
- 		:type s: str
+         """
+         :type s: str
          :rtype: str
- 		"""
- 		pass
+ 
+         Time limit exceeded.
+         """
+         n = len(s)
+         max_len = 1
+         max_i = 0
+         max_j = 0
+         
+         for i in range(n):
+             for j in range(i + 1, n):
+                 if s[i:(j + 1)] == s[i:(j + 1)][::-1]:
+                     ij_len = j - i + 1
+                     if ij_len > max_len:
+                         max_len = ij_len
+                         max_i = i
+                         max_j = j
+                 else:
+                     break
+         return s[max_i:(max_j + 1)]
+ 
+ 
+ class SolutionDP(object):
+     def longestPalindrome(self, s):
+         """
+         :type s: str
+         :rtype: str
+         """
+         pass
  
  
  def main():
- 	s = 'babad'
+     s = 'babad'    # Ans: bab.
- 	print(Solution().longestPalindrome(s))
+     print(SolutionNaive().longestPalindrome(s))
  
- 	s = 'cbbd'
+     s = 'cbbd'     # Ans: bb.
- 	print(Solution().longestPalindrome(s))
+     print(SolutionNaive().longestPalindrome(s))
  
  
  if __name__ == '__main__':
- 	main()
+     main()
  
Complete naive longest palindromic substring
## Code Before:

class Solution(object):
	def longestPalindrome(self, s):
		"""
		:type s: str
        :rtype: str
		"""
		pass


def main():
	s = 'babad'
	print(Solution().longestPalindrome(s))

	s = 'cbbd'
	print(Solution().longestPalindrome(s))


if __name__ == '__main__':
	main()

## Instruction:
Complete naive longest palindromic substring
## Code After:

class SolutionNaive(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str

        Time limit exceeded.
        """
        n = len(s)
        max_len = 1
        max_i = 0
        max_j = 0
        
        for i in range(n):
            for j in range(i + 1, n):
                if s[i:(j + 1)] == s[i:(j + 1)][::-1]:
                    ij_len = j - i + 1
                    if ij_len > max_len:
                        max_len = ij_len
                        max_i = i
                        max_j = j
                else:
                    break
        return s[max_i:(max_j + 1)]


class SolutionDP(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        pass


def main():
    s = 'babad'    # Ans: bab.
    print(SolutionNaive().longestPalindrome(s))

    s = 'cbbd'     # Ans: bb.
    print(SolutionNaive().longestPalindrome(s))


if __name__ == '__main__':
    main()
