LeetCode · Daily Question · 1177. Constructing Palindrome String Detection · Prefix Sum

Author: Xiao Xun
Link: https://leetcode.cn/problems/can-make-palindrome-from-substring/solutions/2309940/qian-zhui-he-zhu-shi-chao-ji-xiang-xi-by- n3ps/
Source: The copyright of LeetCode
belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

topic

 

train of thought

Title -> Given a string, select any position LR in it, you can rearrange any characters and replace any K characters, so that the LR substring is a palindrome string, if it can meet the requirements, it will be TRUE, if not, it will be FALSE.

It is required to be converted into a palindrome string. What is a palindrome string? If the shape is abccba, it is a palindrome string, in which there is a characteristic that the same character is an even number , or on the basis of the former, only one character has an odd number of occurrences.

So now it is simple. The meaning of the question only cares about whether the requirements can be met, not the specific characters. Then you can convert the given string into a string of occurrences of characters, and judge whether a substring can be converted into a palindrome -> judge whether the number of occurrences of characters in the current position meets the above requirements

So how to convert to the above question meaning requirements? Given a substring:

  • If the number of occurrences of the same character is an even number, then this character does not need to be modified
  • If the number of occurrences of the same character is odd:
    • There is only an odd number of occurrences of a character, no need to use the number of modifications
    • If multiple characters appear odd times, you need to use the number of occurrences / 2 modification times to convert the redundant characters to even occurrences

How to count the number of occurrences of each position character?

  • Use an array to record the number of occurrences of characters in each substring
  • Because there are only 26 characters, you can use an int type bit to record the number of occurrences. 0 means even times, and 1 means odd times.
  • The prefix sum can be used, and the current state can be obtained by the difference between the left and right substring states at any position

Code comments are super detailed

the code


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

bool* canMakePaliQueries(char * s, int** queries, int queriesSize, int* queriesColSize, int* returnSize) {
    int n = strlen(s);
    int* count = (int*)malloc((n + 1) * sizeof(int));//二进制代替数组
    memset(count, 0, (n + 1) * sizeof(int));//初始化
    for (int i = 0; i < n; i++) {//前缀和枚举
        // ^ 为不带进位的加法
        count[i + 1] = count[i] ^ (1 << (s[i] - 'a'));//记录整体状态
    }
    bool* res = (bool*)malloc(queriesSize * sizeof(bool));//返回值数组
    for (int i = 0; i < queriesSize; i++) {//枚举子串
        int l = queries[i][0], r = queries[i][1], k = queries[i][2];
        //根据上述表示,大于13则可以能满足转换要求
        //if (k >= 13) {res[i] = true; continue;}
        // 由于没有负值, 那么 0 - 1 等价于 0 + 1
        int bits = 0, x = count[r + 1] ^ count[l];//相差得出当前状态
        while (x > 0) {//求当奇数出现次数
            x &= x - 1;
            bits++;
        }
        res[i] = bits / 2 <= k;//保存有效值
    }
    *returnSize = queriesSize;
    free(count);
    return res;
}



作者:小迅
链接:https://leetcode.cn/problems/can-make-palindrome-from-substring/solutions/2309940/qian-zhui-he-zhu-shi-chao-ji-xiang-xi-by-n3ps/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/m0_64560763/article/details/131224666