Skip to content

Commit 04802fc

Browse files
authored
Create longest_common_palindrome_in_2_strings.py
1 parent a4dfcf4 commit 04802fc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
##function to find the common longest palindrome between 2 strings
2+
##approach is to find the longest palindrome in the shorter string and check if it exists in the longest string
3+
## O(n2)
4+
5+
6+
def longest_common_palindrome(s1, s2):
7+
palindrome_list = []
8+
max_palindrome=""
9+
source = ""
10+
target = ""
11+
if len(s1) > len(s2):
12+
source = s2
13+
target = s1
14+
else:
15+
source = s1
16+
target = s2
17+
for i in range(1, len(source)):
18+
for j in range(0, i):
19+
array = source[j: i + 1]
20+
if array == array[::-1]:
21+
palindrome_list.append(array)
22+
for palindrome in palindrome_list:
23+
if palindrome in target:
24+
if len(palindrome) > len(max_palindrome):
25+
max_palindrome = palindrome
26+
return max_palindrome
27+
28+
print(longest_common_palindrome('xysdjspottTOPSPOTdjhfbsdfajshbd','xysdjasdjhbatopTOPSPOTdjh'))

0 commit comments

Comments
 (0)