1177.can-make-palindrome-from-substring-en
Problem
https://leetcode.com/problems/can-make-palindrome-from-substring/
Problem Description
思路
In this problem, there are 2 conditions for checking whether substring is palindrome:
Rearrange, very useful here. e.g:
abb
can be rearranged to palindrome stringbab
, itself maybe not palindrome, but after rearrange, can be palindrome.Can replace with at most K letters, meaning itself or after rearrange itself still cannot become palindrome, it may be palindrome after replacing K letters. e.g:
abcd, k = 2
,can replacecd -> ba
, to be palindromeabba
; or replaceab -> dc
, be palindromedccd
。
e.g: abcd, k = 1
,then no matter how to replace, it cannot become palindrome.
After analysis, found there is some pattern to form palindrome by rearranging and replace K letters.
If we calculate the number of letters, and track the odd number letters, as oddCounts
, if oddCounts/2 >= K
then we can through replace K letters to form a new palindrome string, otherwise false.
Then this problem becomes to calculate the number of the letter which appears odd times in a substring, queries give us range, so range prefix sum is useful in this case (so that for each given range, we can get the number in ~O(1)
time).
Steps:
Define prefix sum matrix:
prefixCount[i][j]
keep track of in strings
, in range[0, i]
aka, ins[0,i]
, the number of letterj
.Count the number of letters which appears odd number times (
oddCounts
) in range[start, end]
.oddCounts += prefixCount[end+1][currChar] - prefixcount[start][currChar]
。If
oddCounts/2 >= K
, then substring can become palindrome after rearrange and replace<=K
letters.If
oddCounts/2 < K
, then substring cannot become palindrome,return false;
.
As below pic, create a prefix sum matrix, prefixCount[i][j]
, since string only contains 26 lowercase letters, it is O(1)
for loop 26 letters.
Complexity Analysis
Time Complexity:
O(n + m * 26) - n is s length, m is queries length
Space Complexity:
O(n * 26) - n is s length, n * 26 prefixcount matrix
Note, here we can also use HashMap,so that we don't need to loop 26 letters each time, only loop the number of letters in substring.
Validate a String is Palindrome:
Palindrome String: forward and backward are exactly the same, e.g: madam,noon, 12321, aaaa
etc.
Validate Palindrome Solution:
Solution #1: Reverse string s, then check whether original string equals to reversed string.
return s == reverse(s)
Solution #2: use left and right two pointers,
(left, right)
,left
start from index0
,right
from last index,when(left < right)
, compare-
s[left]
==s[right]
, then continue next,left move right, left++
,right move left, right--
s[left]
!=s[right]
,thenreturn false
。
Key Points
prefix sum matrix count index from 0 to i substring, the number of times that
j
letter appearsFor each query, count the number of letters which appears odd times (
oddCounts
) in range[start, end]
, then compareoddCounts
andk
Code (Java/Python3
)
Java/Python3
)Java code
Python3 code (slow :( )
Last updated