Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Can Make Palindrome from Substring

Posted on September 1, 2019July 26, 2020 by braindenny

Can Make Palindrome from Substring



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #palindrome, #presum

Given a string s, we make queries on substrings of s.

For each query queries[i] = [left, right, k], we may rearrange the substring s[left], …, s[right], and then choose up to k 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 an array answer[], where answer[i] is the result of the i-th query queries[i].

Note that: Each letter is counted individually for replacement so if for example s[left..right] = “aaa”, and k = 2, we can only replace two of the letters. (Also, note that the initial string s is never modified by any query.)

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.

Constraints:

  • 1 <= s.length, queries.length <= 10^5
  • 0 <= queries[i][0] <= queries[i][1] < s.length
  • 0 <= queries[i][2] <= s.length
  • s only contains lowercase English letters.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/can-make-palindrome-from-substring
// Basic Ideas: presum
// Complexity: Time O(n), Space O(n)
func canMakePaliQueries(s string, queries [][]int) []bool {
    // add a dummy head node to presum list
    presums := make([][]int, len(s)+1)
    for i, _ := range presums {
        presums[i] = make([]int, 26)
    }
    for i, ch := range s {
        copy(presums[i+1], presums[i])
        presums[i+1][ch-'a']++
    }
    res := make([]bool, len(queries))
    for i, q := range queries {
        left, right, k := q[0], q[1], q[2]
        // compare presums[right+1], presums[left]
        diff := 0
        for j:=0; j<26; j++ {
            diff += (presums[right+1][j] - presums[left][j])%2
        }
        // no need to check whether length is odd or even
        res[i] = (diff/2 <= k)
    }
    return res
}
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #palindrome, presum

Post navigation

LeetCode: Diet Plan Performance
LeetCode: Shortest Common Supersequence

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.