Question3Max. score: 10.00Find the longest substring which is palindrome.Given a string, find the longest substring which is the palindrome. For example, if the given string is “forgeeksskeegfor”, the output should be “geeksskeeg”.Sample inputMadam Arora teaches MalayalamSample outputMalayalamNote:Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.LimitsTime Limit: 5.0 sec(s) for each input fileMemory Limit: 256 MBSource Limit: 1024 KB
Question
Question3Max. score: 10.00Find the longest substring which is palindrome.Given a string, find the longest substring which is the palindrome. For example, if the given string is “forgeeksskeegfor”, the output should be “geeksskeeg”.Sample inputMadam Arora teaches MalayalamSample outputMalayalamNote:Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.LimitsTime Limit: 5.0 sec(s) for each input fileMemory Limit: 256 MBSource Limit: 1024 KB
Solution
The problem is asking to find the longest substring which is a palindrome from a given string. A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.
Here is a Python solution using dynamic programming:
def longest_palindrome(s):
n = len(s)
table = [[0 for x in range(n)]for y in range(n)]
maxLength = 1
i = 0
while (i < n):
table[i][i] = True
i = i + 1
start = 0
i = 0
while i < n - 1:
if (s[i] == s[i + 1]):
table[i][i + 1] = True
start = i
maxLength = 2
i = i + 1
k = 3
while k <= n:
i = 0
while i < (n - k + 1):
j = i + k - 1
if (table[i + 1][j - 1] and s[i] == s[j]):
table[i][j] = True
if (k > maxLength):
start = i
maxLength = k
i = i + 1
k = k + 1
return s[start:start + maxLength]
s = "Madam Arora teaches Malayalam"
s = s.replace(" ", "")
print(longest_palindrome(s.lower()))
This Python code first removes all spaces from the input string and converts it to lowercase. Then it creates a table to store the palindrome status of substrings. It initializes all single character substrings as palindromes and then checks for substrings of length 2. Then it checks for longer substrings. If the current substring is palindrome, it updates the maximum length and the starting index of the longest palindrome found so far. Finally, it returns the longest palindrome substring.
Similar Questions
Find the Longest Palindromic Substring Zoe loves palindromes and she wants to find the longest palindromic substring within a given string. Can you help her find it?Constraints:NAExample:Sample Input:cbbdSample Output:bbExplanation:The longest palindromic substring from the above example is bbPublic Test Cases:# INPUT EXPECTED OUTPUT1 cbbdbb
Write a function to find the longest palindromic substring in a given string
Given a string s, return the longest palindromic substring in s. Example 1:Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer.
Write a Java program to find longest Palindromic Substring within a string CopySample Output:The given string is: thequickbrownfoxxofnworbquicktheThe longest palindrome substring in the given string is; brownfoxxofnworbThe length of the palindromic substring is: 16
Write a Java program to print the smallest and biggest possible palindrome word in a given string. If no palindrome is found in the given string, then, print 'no palindrome'.Input FormatIn the first line read a stringOutput FormatThe first line contains the smallest palindrome word (or) no palindromeThe nextline contains the biggest palindrome word
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.