Conversation
|
|
||
|
|
||
| /** | ||
| create a int array minCut to cache the minimum cuts needed for a palindrome partition for string from position j to the |
There was a problem hiding this comment.
Alright, I am now picky about your explanation here. So you said "minimum cuts needed for a palindrome...from j to the end...", where is that stored? All at minCut[0] or minCut[1]? where is it?
There was a problem hiding this comment.
minCut[j] stores the minimum cuts needed for a palindrome partition for substring from index j to the end of string, thus minCut[0] stands for the minimum cuts needed for a palindrome partition for substring starting with index 0, the entire string.
|
|
||
| minCut[0] is the minimum cuts needed for a palindrome partition for the entire string, namely what we want to return | ||
|
|
||
| create a 2-d boolean array to cache whether string is palindrome from position j to position i |
There was a problem hiding this comment.
Same here. Is it [i][j] or [j][i]?
There was a problem hiding this comment.
substring from j to i
There was a problem hiding this comment.
Still unclear. So what I really want to know is for substring from j to i, will it be stored at [i][j] or [j][i]. You can't always assume [i][j] convention. Be clear.
| for substring from position j to the end: | ||
| try every partition possible i: | ||
| if substring(j, i) is palindrome, as long as minimum cut cached is smaller than 1 + minimum cut for substring(i, end), update it | ||
| edge case: if i reaches to end of string, then no minimum cut is needed for substring(j, i) |
There was a problem hiding this comment.
I don't understand this sentence.
There was a problem hiding this comment.
DP transition function: minCut[j] = Math.min(minCut[j], minCut[i] + 1) && isPalindrome[j][i] == true
where j <= i <= n - 1
The function shows that minimum cuts for string from index j to the end depends on the minimum cuts for string from index i to the end if substring from index j to i is palindrome
| for(int j = n - 1; j >= 0; j--){ | ||
| minCut[j] = n - j - 1; | ||
| for(int i = j; i < n; i++){ | ||
| if(s.charAt(j) == s.charAt(i) && ((j + 1 >= i - 1) || isPalindrome[j + 1][i - 1])){ |
There was a problem hiding this comment.
I don't like the (j+1 >= i-1) here. Because you basically are using edge cases as base cases. More condition in if statement, more likely you will make a mistake during the interview.
There was a problem hiding this comment.
Any idea how I can avoid it?
There was a problem hiding this comment.
So think about this, what is isPalindrome[i][i]? That should be a simple answer and you can initialize that at the beginning. In that case, if you initialize that, can you avoid this if condition?
| for(int i = j; i < n; i++){ | ||
| if(s.charAt(j) == s.charAt(i) && ((j + 1 >= i - 1) || isPalindrome[j + 1][i - 1])){ | ||
| isPalindrome[j][i] = true; | ||
| if(i == n - 1) minCut[j] = 0; |
There was a problem hiding this comment.
See? Another if here and another edge case.
There was a problem hiding this comment.
Think what do you really mean by (i == n - 1)? Actually, you can initialize all minCut to be 0 at beginning. Then you won't have this if statement at all.
No description provided.