LeetCode 1177. Can Make Palindrome from Substring--前缀和

  1. Can Make Palindrome from Substring

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.

Constraints:

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.

题解:先说说暴力处理过程哈,题目说查询[l,r]区间内的字符串,你可以修改其中k个字符变成任意你需要的字符,而且你可以对[l,r]内的字符任意排序,问排序加处理k个字符后的字符串能否构成回文串,这个比较简单了吧,就是判断区间[l,r]内每个字符出现的次数,如果每个字符出现的次数为偶数次不管,如果是奇数次就累计ans++,表明是多出来的没有可以匹配的,如果这个区间[l,r]是奇数长,ans-=1,因为中间那个字符可以单独保留,最后再ans/=2,表示需要处理的对数,如果此时的ans<=k说明足够处理,为true,不然为false,这个思路很好理解吧,然后每次就是从l到r一直累计每个字符出现的次数,判断,然后就超时了,因为肯定超时阿,字符串长度最大为1e5,查询的次数也是1e5,就绝对超时阿,于是我想到只是累计字符出现次数,可以利用前缀和辅助处理,很注意一个细节哈,题目一直强调字符串的每个字符肯定是小写字母a-z,也就是字符类型最多26种,那么我们可以定义num[100100][30]的字符串,num[x][‘a’]就可以表示区间[0,x]中a的出现次数,这个不难理解吧,于是26个字母,时间复杂度1e5*26,完全不会超时呢,于是查询的时候,就是依次判断26个字母出现的次数num[r][char]-num[l-1][char]的次数,思路就是这样了。

AC代码

class Solution {
public:
int num[100100][30];
vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries) 
{
memset(num,0,sizeof(num));
for(int i=0;i<s.length();i++)
{
    int x=s[i]-'a';
    if(i==0)
    num[i][x]=1;
    else 
    {
        for(int j=0;j<26;j++)
        num[i][j]=num[i-1][j];
        num[i][x]=num[i-1][x]+1;
    }
}
vector<bool>res;
for(int i=0;i<queries.size();i++)
{
    int l=queries[i][0],r=queries[i][1],k=queries[i][2];
    int ans=0;
    for(int j=0;j<26;j++)
    {
        if(l==0)
        {
            if(num[r][j]%2==1)ans++;
        }
        else{
            if((num[r][j]-num[l-1][j])%2==1)ans++;
        }
    }
    if((r-l+1)%2==1)ans--;
    ans/=2;
    if(ans<=k)res.push_back(true);
    else res.push_back(false);
}
return res;
}
};

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值