Is there an off-by-one error here?  You have:

```
if c > lens or c > lenx:
    c = min(lens, lenx) - 1
```

If len(s) is 10, and c is 11, you end up setting c to 9.   If len(s) is 10 and c is 10, c remains 10.  That doesn't seem right.