面试题 01.01. 判定字符是否唯一
链接:https://leetcode-cn.com/problems/is-unique-lcci/
def isUnique(astr: str) -> bool:
dic = {}
for s in astr:
if s not in dic.keys():
dic[s] = 1
else:
dic[s] += 1
for v in dic.values():
if v > 1: return False
return True
面试题 01.02. 判定是否互为字符重排
链接:https://leetcode-cn.com/problems/check-permutation-lcci/
def CheckPermutation(s1: str, s2: str) -> bool:
s1 = list(s1)
s1.sort()
s2 = list(s2)
s2.sort()
if s1 == s2: return True
else: return False
s1="abc"
s2="bad"
print(CheckPermutation(s1,s2))
面试题 01.03. URL化
链接:https://leetcode-cn.com/problems/string-to-url-lcci/
def replaceSpaces(S: str, length: int) -> str:
'''利用 replace 进行替换'''
return S[:length].replace(" ", "%20")
面试题 01.06. 字符串压缩
链接:https://leetcode-cn.com/problems/compress-string-lcci/
def compressString(S: str) -> str:
'''双指针'''
if len(S)==0:return S
i = 0
j = 0
ans = ''
for j in range(len(S)):
if S[j] != S[i]:
ans = ans + S[i] + str(j-i)
i = j
ans = ans + S[i] + str(j-i+1)
return S if len(ans) >= len(S) else ans
S = "aabcccccaaa"
print(compressString(S))
面试题 01.09. 字符串轮转
链接:https://leetcode-cn.com/problems/string-rotation-lcci/
def isFlipedString(s1: str, s2: str) -> bool:
'''
如果一个字符串是另一个字符串的旋转,那么它就是在某个特定点上的旋转。
例,字符串waterbottle在3处的旋转意味着在第三个字符处切割waterbottle,
并在左半部分(wat)之前放置右半部分(erbottle)。
'''
if len(s1) != len(s2): return False
if len(s1) == len(s2) == 0: return True
for i in range(0, len(s1)):
a = s1[:i]
b = s1[i:]
ans = b + a
if ans == s2: return True
else:
return False