某个子串可以重复,那么整个字符串的长度一定是子串长度的整数倍,并且子串周期性的重复
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
s_length=len(s)
for i in range(1,s_length//2+1):
#整数倍
if s_length%i==0:
#周期性的重复
if all(s[j]==s[j-i] for j in range(i,s_length)):
return True
return False
2 单词规律
建立匹配串和字符串之间的映射,相同的模式则相同的字符串应该对应到相同的匹配串字符上
3 反转字符串中的元音字母双指针搜索元音字符进行交换反转
class Solution:
def reverseVowels(self, s: str) -> str:
#判断是否是元音
def isVowel(ch: str) -> bool:
return ch in "aeiouAEIOU"
n = len(s)
s = list(s)
#双指针
i, j = 0, n - 1
while i < j:
while i < n and not isVowel(s[i]):
i += 1
while j > 0 and not isVowel(s[j]):
j -= 1
#交换
if i < j:
s[i], s[j] = s[j], s[i]
i += 1
j -= 1
return "".join(s)



