Skip to content

Commit be06cef

Browse files
authored
Update longest_common_palindrome_in_2_strings.py
1 parent 264ecca commit be06cef

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

longest_common_palindrome_in_2_strings.py

+14
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ def longest_common_palindrome(s1, s2):
2626
return max_palindrome
2727

2828
print(longest_common_palindrome('xysdjspottTOPSPOTdjhfbsdfajshbd','xysdjasdjhbatopTOPSPOTdjh'))
29+
30+
####OR simple
31+
32+
def longest_palindrome(s1, s2):
33+
if len(s1) > len(s2):
34+
s1, s2 = s2, s1
35+
longest = ''
36+
for i in range(len(s1)):
37+
for j in range(i+1, len(s1)+1):
38+
if s1[i:j] in s2 and len(s1[i:j]) > len(longest):
39+
longest = s1[i:j]
40+
return longest
41+
42+
print(longest_palindrome('rabc', 'abacdfgdcaba')) #ab

0 commit comments

Comments
 (0)