Skip to main content
added 8 characters in body; edited tags
Source Link
esote
  • 3.8k
  • 2
  • 23
  • 44

I have encountered to this question and applied a brute force algorithm off top of my head and couldn't find a better optimized solution.

I was hoping I could get some insight to implement a better optimized code in terms of complexity. My solution has O(N2) as well as O(n2)\$O(n^2)\$ time and space complexity.

class Solution:

    def longestPalindrome(self, s: 'str') -> 'str':
        if s=="":
            return ""
        else:
           ## Find all the substrings
            arr=[]
            for i in range(len(s)):
                char = s[i]
                arr.append(char)
                for j in range(i+1, len(s)-1):
                    char+=s[j]
                    arr.append(char)

            ##Find the palindrome with a longest length
            max_length = 0
            for j in range(len(arr)):
                if self.isPalindrome(arr[j]):
                    if len(arr[j]) > max_length:
                        max_length = len(arr[j])
                        index = arr[j]
            return index

        
    def isPalindrome(self,s:'str')->'str':
        if s == s[::-1]:
            return True

I have encountered to this question and applied a brute force algorithm off top of my head and couldn't find a better optimized solution.

I was hoping I could get some insight to implement a better optimized code in terms of complexity. My solution has O(N2) as well as O(n2) space.

class Solution:

    def longestPalindrome(self, s: 'str') -> 'str':
        if s=="":
            return ""
        else:
           ## Find all the substrings
            arr=[]
            for i in range(len(s)):
                char = s[i]
                arr.append(char)
                for j in range(i+1, len(s)-1):
                    char+=s[j]
                    arr.append(char)

            ##Find the palindrome with a longest length
            max_length = 0
            for j in range(len(arr)):
                if self.isPalindrome(arr[j]):
                    if len(arr[j]) > max_length:
                        max_length = len(arr[j])
                        index = arr[j]
            return index

        
    def isPalindrome(self,s:'str')->'str':
        if s == s[::-1]:
            return True

I have encountered to this question and applied a brute force algorithm off top of my head and couldn't find a better optimized solution.

I was hoping I could get some insight to implement a better optimized code in terms of complexity. My solution has \$O(n^2)\$ time and space complexity.

class Solution:

    def longestPalindrome(self, s: 'str') -> 'str':
        if s=="":
            return ""
        else:
           ## Find all the substrings
            arr=[]
            for i in range(len(s)):
                char = s[i]
                arr.append(char)
                for j in range(i+1, len(s)-1):
                    char+=s[j]
                    arr.append(char)

            ##Find the palindrome with a longest length
            max_length = 0
            for j in range(len(arr)):
                if self.isPalindrome(arr[j]):
                    if len(arr[j]) > max_length:
                        max_length = len(arr[j])
                        index = arr[j]
            return index

        
    def isPalindrome(self,s:'str')->'str':
        if s == s[::-1]:
            return True
added 114 characters in body
Source Link
vnp
  • 57.3k
  • 4
  • 51
  • 140

I have encountered to this question and applied a brute force algorithm off top of my head and couldn't find a better optimized solution.

I was hoping I could get some insight to implement a better optimized code in terms of complexity. My solution has O(N2) as well as O(n2) space.

class Solution:

class Solution:

    def longestPalindrome(self, s: 'str') -> 'str':
        if s=="":
            return ""
        else:
           ## Find all the substrings
            arr=[]
            for i in range(len(s)):
                char = s[i]
                arr.append(char)
                for j in range(i+1, len(s)-1):
                    char+=s[j]
                    arr.append(char)

            ##Find the palindrome with a longest length
            max_length = 0
            for j in range(len(arr)):
                if self.isPalindrome(arr[j]):
                    if len(arr[j]) > max_length:
                        max_length = len(arr[j])
                        index = arr[j]
            return index

        
    def isPalindrome(self,s:'str')->'str':
        if s == s[::-1]:
            return True

I have encountered to this question and applied a brute force algorithm off top of my head and couldn't find a better optimized solution.

I was hoping I could get some insight to implement a better optimized code in terms of complexity. My solution has O(N2) as well as O(n2) space.

class Solution:

def longestPalindrome(self, s: 'str') -> 'str':
    if s=="":
        return ""
    else:
       ## Find all the substrings
        arr=[]
        for i in range(len(s)):
            char = s[i]
            arr.append(char)
            for j in range(i+1, len(s)-1):
                char+=s[j]
                arr.append(char)

        ##Find the palindrome with a longest length
        max_length = 0
        for j in range(len(arr)):
            if self.isPalindrome(arr[j]):
                if len(arr[j]) > max_length:
                    max_length = len(arr[j])
                    index = arr[j]
        return index

    
def isPalindrome(self,s:'str')->'str':
    if s == s[::-1]:
        return True

I have encountered to this question and applied a brute force algorithm off top of my head and couldn't find a better optimized solution.

I was hoping I could get some insight to implement a better optimized code in terms of complexity. My solution has O(N2) as well as O(n2) space.

class Solution:

    def longestPalindrome(self, s: 'str') -> 'str':
        if s=="":
            return ""
        else:
           ## Find all the substrings
            arr=[]
            for i in range(len(s)):
                char = s[i]
                arr.append(char)
                for j in range(i+1, len(s)-1):
                    char+=s[j]
                    arr.append(char)

            ##Find the palindrome with a longest length
            max_length = 0
            for j in range(len(arr)):
                if self.isPalindrome(arr[j]):
                    if len(arr[j]) > max_length:
                        max_length = len(arr[j])
                        index = arr[j]
            return index

        
    def isPalindrome(self,s:'str')->'str':
        if s == s[::-1]:
            return True
Source Link
Ugur Yilmaz
  • 383
  • 3
  • 10

Longest Palindromic Substring better approach

I have encountered to this question and applied a brute force algorithm off top of my head and couldn't find a better optimized solution.

I was hoping I could get some insight to implement a better optimized code in terms of complexity. My solution has O(N2) as well as O(n2) space.

class Solution:

def longestPalindrome(self, s: 'str') -> 'str':
    if s=="":
        return ""
    else:
       ## Find all the substrings
        arr=[]
        for i in range(len(s)):
            char = s[i]
            arr.append(char)
            for j in range(i+1, len(s)-1):
                char+=s[j]
                arr.append(char)

        ##Find the palindrome with a longest length
        max_length = 0
        for j in range(len(arr)):
            if self.isPalindrome(arr[j]):
                if len(arr[j]) > max_length:
                    max_length = len(arr[j])
                    index = arr[j]
        return index

    
def isPalindrome(self,s:'str')->'str':
    if s == s[::-1]:
        return True