File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
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' ))
You can’t perform that action at this time.
0 commit comments