Skip to content

Instantly share code, notes, and snippets.

@VinayakBagaria
Created June 6, 2020 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VinayakBagaria/ef60cb007732ab3e10b2a61db6edaa74 to your computer and use it in GitHub Desktop.
Save VinayakBagaria/ef60cb007732ab3e10b2a61db6edaa74 to your computer and use it in GitHub Desktop.
longest palin
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')
@RajibKDey
Copy link

def longestPalindrome(s):
print(s)
length = len(s)
l = [[1] * length] * length

for i in range(1,length + 1):
    for j in range(0, length - i + 1):
        k = j+i
        # print(s[j:k])
        main_string = s[j:k]
        if(len(main_string) >= 2):
            if(len(main_string) == 2):
                if(main_string[0] == main_string[1]):
                    l[j][k-1] = 2
                    # print(main_string, 2)
                    # print('x', j, 'y', k - 1)
                else:
                    l[j][k-1] = 1
                    # print(main_string, 1)
                    # print('x', j, 'y', k - 1)
            else:
                if(main_string[0] == main_string[len(main_string)-1]):
                    if(l[j+1][k-2] == 1):
                        if(len(main_string) == 3):
                            l[j][k-1] = 3
                            # print(main_string, 3)
                            # print('x', j, 'y', k - 1)
                        else:
                            l[j][k-1] = 1
                            # print(main_string, 1)
                            # print('x', j, 'y', k - 1)
                    else:
                        l[j][k-1] = len(main_string)
                        # print(main_string, len(main_string))
                        # print('x', j, 'y', k - 1)
                else:
                    continue
                    # print(main_string, 1)
                    # print('x', j, 'y', k - 1)
        else:
            l[j][k-1] = 1
            # print(main_string, 1)
            # print('x', j, 'y', k-1)
        print(s[j:k])
        for p in range(length):
            for q in range(length):
                print(l[p][q], end=' ')
            print()
        print()

# Todo: for some reason repeats the same row 8 times. problem probably lies in l array manipulation
# for i in range(length):
#     print()
#     for j in range(length):
#         print(l[i][j], end=' ')

longestPalindrome('ABBAACBA')

@RajibKDey
Copy link

RajibKDey commented Jun 6, 2020

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