I had an interview, where I was asked to solve to this question:
Given an string str, find the maximum length of palindrome subsequence.
Example: >if str = 'abcda':
maximum_length = 3 'aca'
How can I improve this code?
def maximum_length_palindrome_subsequence(a):
a = list(a)
n = len(a)
table = [[0 for i in xrange(n)] for j in xrange(n)]
for i in xrange(n):
table[i][i] = 1
for gap in xrange(1,n):
for i in xrange(n-1):
h = i+gap
if h>=n:
continue
if a[i] == a[h]:
if i+1==h:
table[i][h] = 2
else:
table[i][h] = table[i+1][h-1] + 2
else:
if h<n:
table[i][h] = max(table[i+1][h], table[i][h-1])
return table[0][n-1]
print maximum_length_palindrome_subsequence('abcbda')
I had an interview, where I was asked to solve to this question:
Given an string str, find the maximum length of palindrome subsequence.
Example: >if str = 'abcda':
maximum_length = 3 'aca'
How can I improve this code?
def maximum_length_palindrome_subsequence(a):
a = list(a)
n = len(a)
table = [[0 for i in xrange(n)] for j in xrange(n)]
for i in xrange(n):
table[i][i] = 1
for gap in xrange(1,n):
for i in xrange(n-1):
h = i+gap
if h>=n:
continue
if a[i] == a[h]:
if i+1==h:
table[i][h] = 2
else:
table[i][h] = table[i+1][h-1] + 2
else:
if h<n:
table[i][h] = max(table[i+1][h], table[i][h-1])
return table[0][n-1]
print maximum_length_palindrome_subsequence('abcbda')
I had an interview, where I was asked to solve to this question:
Given an string str, find the maximum length of palindrome subsequence.
Example: >if str = 'abcda':
maximum_length = 3 'aca'
How can I improve this code?
def maximum_length_palindrome_subsequence(a):
a = list(a)
n = len(a)
table = [[0 for i in xrange(n)] for j in xrange(n)]
for i in xrange(n):
table[i][i] = 1
for gap in xrange(1,n):
for i in xrange(n-1):
h = i+gap
if h>=n:
continue
if a[i] == a[h]:
if i+1==h:
table[i][h] = 2
else:
table[i][h] = table[i+1][h-1] + 2
else:
if h<n:
table[i][h] = max(table[i+1][h], table[i][h-1])
return table[0][n-1]
print maximum_length_palindrome_subsequence('abcbda')
length Length of longest palindrome subsequence
I had an interview, where I was asked to solve to this question:
Given an string str, find the maximum length of palindrome subsequence.
Given an string str, find the maximum length of palindrome subsequence.
Example: >if str = 'abcda':
maximum_length = 3 'aca'
How tocan I improve this code?
def maximum_length_palindrome_subsequence(a):
a = list(a)
n = len(a)
table = [[0 for i in xrange(n)] for j in xrange(n)]
for i in xrange(n):
table[i][i] = 1
for gap in xrange(1,n):
for i in xrange(n-1):
h = i+gap
if h>=n:
continue
if a[i] == a[h]:
if i+1==h:
table[i][h] = 2
else:
table[i][h] = table[i+1][h-1] + 2
else:
if h<n:
table[i][h] = max(table[i+1][h], table[i][h-1])
return table[0][n-1]
print maximum_length_palindrome_subsequence('abcbda')
length of longest palindrome subsequence
I had an interview, where I was asked to solve to this question:
Given an string str, find the maximum length of palindrome subsequence.
Example: >if str = 'abcda':
maximum_length = 3 'aca'
How to improve code?
def maximum_length_palindrome_subsequence(a):
a = list(a)
n = len(a)
table = [[0 for i in xrange(n)] for j in xrange(n)]
for i in xrange(n):
table[i][i] = 1
for gap in xrange(1,n):
for i in xrange(n-1):
h = i+gap
if h>=n:
continue
if a[i] == a[h]:
if i+1==h:
table[i][h] = 2
else:
table[i][h] = table[i+1][h-1] + 2
else:
if h<n:
table[i][h] = max(table[i+1][h], table[i][h-1])
return table[0][n-1]
print maximum_length_palindrome_subsequence('abcbda')
Length of longest palindrome subsequence
I had an interview, where I was asked to solve to this question:
Given an string str, find the maximum length of palindrome subsequence.
Example: >if str = 'abcda':
maximum_length = 3 'aca'
How can I improve this code?
def maximum_length_palindrome_subsequence(a):
a = list(a)
n = len(a)
table = [[0 for i in xrange(n)] for j in xrange(n)]
for i in xrange(n):
table[i][i] = 1
for gap in xrange(1,n):
for i in xrange(n-1):
h = i+gap
if h>=n:
continue
if a[i] == a[h]:
if i+1==h:
table[i][h] = 2
else:
table[i][h] = table[i+1][h-1] + 2
else:
if h<n:
table[i][h] = max(table[i+1][h], table[i][h-1])
return table[0][n-1]
print maximum_length_palindrome_subsequence('abcbda')