1.实现一个算法:识别一个字符串中,是否包含唯一的字符:
# --1.实现一个算法:识别一个字符串中,是否包含唯一的字符。
# 如果字符串中的字符都是唯一的,则返回 True,如 '123'。
# 如果字符串中的字符有重复,则返回 False,如 '1223'。
def has_unique_chars(string):
if string is None:
return False
return len(set(string)) == len(string)
print(has_unique_chars("123454"))
2.实现一个算法来识别一个字符串 str2 是否是另一个字符串 str1 的排列:
def is_permutation(str1, str2):
if str1 is None or str2 is None:
return False
return sorted(str1) == sorted(str2)
print(is_permutation("abc","cacb"))
3.实现一个算法来实现反转字符数组的功能。
def reverse(chars):
if chars:
size = len(chars)
for i in range(size // 2):
chars[i], chars[size - 1 - i] = \
chars[size - 1 - i], chars[i]
return chars