|
发表于 2023-4-22 15:22:27
|
显示全部楼层
- def is_neighbor_string(str1, str2):
- if len(str1) != len(str2):
- return False
- diff_count = 0
- for i in range(len(str1)):
- if str1[i] != str2[i]:
- diff_count += 1
- if diff_count > 1:
- return False
- return diff_count == 1
- # 测试代码
- str1 = "read"
- str2 = "rexd"
- print(is_neighbor_string(str1, str2)) # 输出:True
复制代码
或
- def is_close(str1, str2):
- if len(str1) != len(str2):
- return False
- cnt = 0
- for i in range(len(str1)):
- if str1[i] != str2[i]:
- cnt += 1
- if cnt > 1:
- return False
- return cnt == 1
- str1, str2 = input().split()
- print(is_close(str1, str2))
复制代码 |
|