[leetcode] 1177. Can Make Palindrome from Substring

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. 1 <= s.length, queries.length <= 10^5.
  2. 0 <= queries[i][0] <= queries[i][1] < s.length.
  3. 0 <= queries[i][2] <= s.length.
  4. 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)

参考文献

[LeetCode] short and fast C++ prefix sum problem

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值