def first_diff_index(s1, s2):
"""
返回第一个位置的索引,在这个位置上两个字符串不同。如果这两个字符串是相同的,它应该返回-1。
"""
for i in range(min(len(s1), len(s2))):
if s1[i] != s2[i]:
return i
if len(s1) != len(s2):
return min(len(s1), len(s2))
return -1
使用示例:
>>> first_diff_index("hello", "world")
0
>>> first_diff_index("hello", "hella")
4
>>> first_diff_index("python", "python")
-1