LeetCode 1177. Can Make Palindrome from Substring Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1177. Can Make Palindrome from Substring

Description

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.

Solutions

Solution 1: Prefix Sum

First, consider whether a substring can become a palindrome after at most k replacements. Obviously, we need to count the number of times each character appears in the substring, which can be implemented through prefix sum. For characters that appear an even number of times, we do not need to replace them. For characters that appear an odd number of times, we need to replace them. The number of replacements is \lfloor x2 \rfloor, where x is the number of characters that appear an odd number of times. If \lfloor x2 \rfloor ≤ k, then this substring can become a palindrome.

Therefore, we define a prefix sum array ss, where ss[i][j] represents the number of times character j appears in the first i characters of string s. Then for a substring s[l..r], we can get the number of times character j appears in the substring through ss[r + 1][j] - ss[l][j]. We traverse all queries. For each query [l, r, k], we count the number of characters x that appear an odd number of times in the substring s[l..r]. If \lfloor x2 \rfloor ≤ k, then this substring can become a palindrome.

The time complexity is O((n + m) × C), and the space complexity is O(n × C). Here, n and m are the lengths of the string s and the query array respectively; and C is the size of the character set. In this problem, the character set is lowercase English letters, so C = 26.

PythonJavaC++GoTypeScript
class Solution: def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]: n = len(s) ss = [[0] * 26 for _ in range(n + 1)] for i, c in enumerate(s, 1): ss[i] = ss[i - 1][:] ss[i][ord(c) - ord("a")] += 1 ans = [] for l, r, k in queries: cnt = sum((ss[r + 1][j] - ss[l][j]) & 1 for j in range(26)) ans.append(cnt // 2 <= k) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !