|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/diet-plan-performance "Diet Plan Performance") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/number-of-valid-words-for-each-puzzle "Number of Valid Words for Each Puzzle") |
| 11 | + |
| 12 | +## 5175. Can Make Palindrome from Substring (Medium) |
| 13 | + |
| 14 | +<p>Given a string <code>s</code>, we make queries on substrings of <code>s</code>.</p> |
| 15 | + |
| 16 | +<p>For each query <code>queries[i] = [left, right, k]</code>, we may <strong>rearrange</strong> the substring <code>s[left], ..., s[right]</code>, and then choose <strong>up to</strong> <code>k</code> of them to replace with any lowercase English letter. </p> |
| 17 | + |
| 18 | +<p>If the substring is possible to be a palindrome string after the operations above, the result of the query is <code>true</code>. Otherwise, the result is <code>false</code>.</p> |
| 19 | + |
| 20 | +<p>Return an array <code>answer[]</code>, where <code>answer[i]</code> is the result of the <code>i</code>-th query <code>queries[i]</code>.</p> |
| 21 | + |
| 22 | +<p>Note that: Each letter is counted <strong>individually</strong> for replacement so if for example <code>s[left..right] = "aaa"</code>, and <code>k = 2</code>, we can only replace two of the letters. (Also, note that the initial string <code>s</code> is never modified by any query.)</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong>Example :</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]] |
| 29 | +<strong>Output:</strong> [true,false,false,true,true] |
| 30 | +<strong>Explanation:</strong> |
| 31 | +queries[0] : substring = "d", is palidrome. |
| 32 | +queries[1] : substring = "bc", is not palidrome. |
| 33 | +queries[2] : substring = "abcd", is not palidrome after replacing only 1 character. |
| 34 | +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". |
| 35 | +queries[4] : substring = "abcda", could be changed to "abcba" which is palidrome. |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= s.length, queries.length <= 10^5</code></li> |
| 43 | + <li><code>0 <= queries[i][0] <= queries[i][1] < s.length</code></li> |
| 44 | + <li><code>0 <= queries[i][2] <= s.length</code></li> |
| 45 | + <li><code>s</code> only contains lowercase English letters.</li> |
| 46 | +</ul> |
| 47 | + |
| 48 | +### Related Topics |
| 49 | + [[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)] |
| 50 | + [[String](https://github.com/openset/leetcode/tree/master/tag/string/README.md)] |
| 51 | + |
| 52 | +### Hints |
| 53 | +<details> |
| 54 | +<summary>Hint 1</summary> |
| 55 | +Since we can rearrange the substring, all we care about is the frequency of each character in that substring. |
| 56 | +</details> |
| 57 | + |
| 58 | +<details> |
| 59 | +<summary>Hint 2</summary> |
| 60 | +How to find the character frequencies efficiently ? |
| 61 | +</details> |
| 62 | + |
| 63 | +<details> |
| 64 | +<summary>Hint 3</summary> |
| 65 | +As a preprocess, calculate the accumulate frequency of all characters for all prefixes of the string. |
| 66 | +</details> |
| 67 | + |
| 68 | +<details> |
| 69 | +<summary>Hint 4</summary> |
| 70 | +How to check if a substring can be changed to a palindrome given its characters frequency ? |
| 71 | +</details> |
| 72 | + |
| 73 | +<details> |
| 74 | +<summary>Hint 5</summary> |
| 75 | +Count the number of odd frequencies, there can be at most one odd frequency in a palindrome. |
| 76 | +</details> |
0 commit comments