Skip to content

#132#4

Open
yuewang0319 wants to merge 4 commits intomasterfrom
132
Open

#132#4
yuewang0319 wants to merge 4 commits intomasterfrom
132

Conversation

@yuewang0319
Copy link
Copy Markdown
Collaborator

No description provided.



/**
create a int array minCut to cache the minimum cuts needed for a palindrome partition for string from position j to the
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's better.


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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Is it [i][j] or [j][i]?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

substring from j to i

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this sentence.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])){
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea how I can avoid it?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See? Another if here and another edge case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any hint?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants