{x}
blog image

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.

Solution Explanation for 1177. Can Make Palindrome from Substring

This problem asks whether a substring of a given string s can be rearranged to form a palindrome after replacing at most k characters. The solution leverages prefix sums for efficient computation.

Approach:

  1. Prefix Sum Array: We create a 2D prefix sum array ss of size (n+1) x 26, where n is the length of s. ss[i][j] stores the count of character j (represented by its ASCII value - 'a') in the substring s[0...i-1]. This allows us to quickly calculate the character counts within any substring s[l...r] using ss[r+1][j] - ss[l][j].

  2. Query Processing: For each query [l, r, k], we iterate through the alphabet (0 to 25). We count the number of characters with odd counts in the substring s[l...r] using the prefix sum array. Let's call this count x.

  3. Palindrome Check: A string can be rearranged into a palindrome if and only if at most one character has an odd count. If x is the number of characters with odd counts, we need to replace at least x/2 characters to make the substring a palindrome. If x/2 is less than or equal to k (the maximum allowed replacements), the query returns true; otherwise, it returns false.

Time and Space Complexity:

  • Time Complexity: O((n + m) * 26), where n is the length of string s and m is the number of queries. The prefix sum array creation takes O(n * 26) time. Processing each query takes O(26) time.

  • Space Complexity: O(n * 26). The prefix sum array ss dominates the space complexity.

Code Implementation (Python):

class Solution:
    def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
        n = len(s)
        ss = [[0] * 26 for _ in range(n + 1)] #Prefix sum array initialization
 
        for i, c in enumerate(s, 1): #Build the prefix sum array
            ss[i] = ss[i - 1][:]
            ss[i][ord(c) - ord("a")] += 1
 
        ans = []
        for l, r, k in queries: #Process each query
            cnt = sum((ss[r + 1][j] - ss[l][j]) & 1 for j in range(26)) #Count characters with odd occurrences
            ans.append(cnt // 2 <= k) #Check if it can be made a palindrome with at most k replacements
 
        return ans
 

The implementations in other languages (Java, C++, Go, TypeScript) follow a very similar structure, differing only in syntax and data structure specifics. They all utilize the same core algorithm of prefix sum computation and query processing.