Skip to content

Instantly share code, notes, and snippets.

@moonwatcher
Created August 6, 2021 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moonwatcher/c225975e00b64486c86ea46f0b1d3463 to your computer and use it in GitHub Desktop.
Save moonwatcher/c225975e00b64486c86ea46f0b1d3463 to your computer and use it in GitHub Desktop.
#!/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