Created
August 6, 2021 17:55
-
-
Save moonwatcher/c225975e00b64486c86ea46f0b1d3463 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# | |
# abcMdefggfedcba | |
# bcMdefggfedcb | |
# cMdefggfedc | |
# Mdefggfed | |
# defggfed | |
# Mdefggfe | |
def palindromeIndex(s): | |
# Write your code here | |
result = -1 | |
n = len(s) | |
if n > 1: | |
l = 0 | |
r = len(s) - 1 | |
while l < r: | |
if s[l] != s[r]: | |
if is_plaindrome(s[l+1:r+1]): | |
result = l | |
elif is_plaindrome(s[l:r]): | |
result = r | |
break | |
l+=1 | |
r-=1 | |
return result | |
def is_plaindrome(s): | |
result = True | |
if len(s) > 1: | |
l = 0 | |
r = len(s) - 1 | |
while l < r: | |
if s[l] == s[r]: | |
l+=1 | |
r-=1 | |
else: | |
result = False | |
break | |
return result | |
print(palindromeIndex('hgygsvlfcwnswtuhmyaljkqlqjjqlqkjlaymhutwsnwcwflvsgygh')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment