I've got a doubt regarding substring matching can anyone help

suppose i’ve a string as a “abc cde efg” i want if the last character of previous substring and first character of current substring matches print possible i’ve written following code can anyone modify the code

        s=input()
n=3
for i in range(len(s)):
    if(i%n==0):
        if(s[i]==s[i+2]):
            flag=1
        else:
            flag=0
    else:
        flag=-1
if(flag==1):
    print("possible")
elif(flag==0):
    print("impossible")
else:
    print("No")
s=input()
n=3
for i in range(len(s)):
 for j in range(i+n-1,len(s)-2):
    if(i%n==0):
        if(s[i]==s[j]):
            flag=1
            continue
        else:
            flag=0
            continue
    else:
        flag=-1
if(flag==1):
    print("possible")
elif(flag==0):
    print("impossible")
else:
    print("No")