Description
Given a string s, we make queries on substrings of s.
For each query queries[i] = [left, right, k], we may rearrange the substring s[left], …, s[right], and then choose up to k of them to replace with any lowercase English letter.
If the substring is possible to be a palindrome string after the operations above, the result of the query is true. Otherwise, the result is false.
Return an array answer[], where answer[i] is the result of the i-th query queries[i].
Note that: Each letter is counted individually for replacement so if for example s[left…right] = “aaa”, and k = 2, we can only replace two of the letters. (Also, note that the initial string s is never modified by any query.)
Example:
Input: s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
Output: [true,false,false,true,true]
Explanation:
queries[0] : substring = "d", is palidrome.
queries[1] : substring = "bc", is not palidrome.
queries[2] : substring = "abcd", is not palidrome after replacing only 1 character.
queries[3] : substring = "abcd", could be changed to "abba" which is palidrome. Also this can be changed to "baab" first rearrange it "bacd" then replace "cd" with "ab".
queries[4] : substring = "abcda", could be changed to "abcba" which is palidrome.
Note:
- 1 <= s.length, queries.length <= 10^5.
- 0 <= queries[i][0] <= queries[i][1] < s.length.
- 0 <= queries[i][2] <= s.length.
- s only contains lowercase English letters.
分析
题目的意思是:给定一个字符串和一个query数组,找出每个query下是否能够成回文子串。这道题我尝试实现了一下,最终还是败给了一些case,于是参考了别人C++的实现,首先创建一个dp数组,统计字符串s每个字符的次数,然后遍历query,类似prefix sum的做法,这里用dp数组来实现,先把所有的组合形式列举出来,然后一个一个的判断。这里Exmple的dp数组有一点混淆,我把dp的计算结果列了出来, 其实就是每个字符的统计结果
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
代码
class Solution:
def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
ans=[]
d=len(s)
n=len(queries)
dp=[[0 for i in range(26)] for j in range(d+1)]
for i in range(d):
for j in range(26):
dp[i+1][j]=dp[i][j]
dp[i+1][ord(s[i])-ord('a')]+=1
for q in queries:
start=q[0]
end=q[1]
k=q[2]
cnt=0
for i in range(26):
cnt += (dp[end+1][i] - dp[start][i])%2
ans.append(cnt//2 <= k)
return ans
统计代码
class Solution:
def canMakePaliQueries(self, s: str, queries: list):
ans=[]
d=len(s)
n=len(queries)
dp=[[0 for i in range(26)] for j in range(d+1)]
for i in range(d):
for j in range(26):
dp[i+1][j]=dp[i][j]
dp[i+1][ord(s[i])-ord('a')]+=1
print(dp)
for q in queries:
start=q[0]
end=q[1]
k=q[2]
cnt=0
for i in range(26):
cnt += (dp[end+1][i] - dp[start][i])%2
ans.append(cnt//2 <= k)
return ans
if __name__ == "__main__":
solution=Solution()
s="abcda"
arr=[[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
ans=solution.canMakePaliQueries(s,arr)
print(ans)