LeetCode每日一题(1177. Can Make Palindrome from Substring)

You are given a string s and array queries where queries[i] = [lefti, righti, ki]. We may rearrange the substring s[lefti…righti] for each query and then choose up to ki 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 a boolean array answer where answer[i] is the result of the ith query queries[i].

Note that each letter is counted individually for replacement, so if, for example s[lefti…righti] = “aaa”, and ki = 2, we can only replace two of the letters. Also, note that no query modifies the initial string s.

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.

Example 2:

Input: s = “lyb”, queries = [[0,1,0],[2,2,1]]
Output: [false,true]

Constraints:

  • 1 <= s.length, queries.length <= 105
  • 0 <= lefti <= righti < s.length
  • 0 <= ki <= s.length
  • s consists of lowercase English letters.

We may rearrange the substring s[lefti…righti] for each query and then choose up to ki of them to replace with any lowercase English letter.
注意 rearrange 和 replace 是两步操作


impl Solution {
    pub fn can_make_pali_queries(s: String, queries: Vec<Vec<i32>>) -> Vec<bool> {
        let mut prefix_count: Vec<Vec<i32>> = s
            .chars()
            .scan(vec![0; 26], |s, c| {
                s[c as usize - 97] += 1;
                Some(s.clone())
            })
            .collect();
        prefix_count.insert(0, vec![0; 26]);
        let mut ans = Vec::new();
        for q in queries {
            let mut odd_count = 0;
            for i in 0..26 {
                let count = prefix_count[q[1] as usize + 1][i] - prefix_count[q[0] as usize][i];
                if count % 2 != 0 {
                    odd_count += 1;
                }
            }
            if (q[1] - q[0]) % 2 == 1 {
                if q[2] >= odd_count / 2 + odd_count % 2 {
                    ans.push(true);
                    continue;
                }
                ans.push(false);
                continue;
            }
            if q[2] >= (odd_count - 1).abs() / 2 + (odd_count - 1).abs() % 2 {
                ans.push(true);
                continue;
            }
            ans.push(false);
        }
        ans
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值