Created
June 6, 2020 00:12
-
-
Save VinayakBagaria/ef60cb007732ab3e10b2a61db6edaa74 to your computer and use it in GitHub Desktop.
longest palin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longestPalindrome(s): | |
print(s) | |
length = len(s) | |
l = [[1] * length] * length | |
# 'ABBAACBA' | |
# i => 1 | |
# j => 5 | |
# i=0 j=0, i=1 j=1 | |
# i=0 j=1, i=1 j=2 | |
# i=0 j=2 i=1 j=3 | |
for j in range(1, length): | |
for i in range(length - j + 1): | |
main_string = s[i: i + j] | |
substring = s[i + 1: i + j - 1] | |
if main_string[0] == main_string[len(main_string) - 1]: | |
# if main_string == 'BB': | |
# print(i, 'Bb', j) | |
# if main_string == 'ABBA': | |
# print(i, 'x', j) | |
# print(l[i + 1][j - 2]) | |
if len(substring) == 0: | |
l[j][i] = len(main_string) | |
print('subs',j, i, main_string) | |
# elif l[j + 1][i - 1] > 1: | |
# print("True") | |
# l[j][i] = len(main_string) | |
# continue | |
l[j][i] = 1 | |
# print(l) | |
longestPalindrome('ABBAACBA') |
error not fixed completely but I feel the solution is real close. Maybe you can try my approach but try with the substring procedure else you have to test stupid amounts of edge cases
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def longestPalindrome(s):
print(s)
length = len(s)
l = [[1] * length] * length
longestPalindrome('ABBAACBA')