-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added code for palindrome partitioning problem under dynamic programming #7222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ae7c99
Added code for palindrome partitioning problem under dynamic programming
agnivg 9690b39
Updated return type for function
agnivg 5d5be65
Updated Line 24 according to suggestions
agnivg 370c279
Apply suggestions from code review
cclauss 34ead4d
Update palindrome_partitioning.py
cclauss c886e31
Update palindrome_partitioning.py
cclauss 048d023
is_palindromic
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Given a string s, partition s such that every substring of the | ||
partition is a palindrome. | ||
Find the minimum cuts needed for a palindrome partitioning of s. | ||
|
||
Time Complexity: O(n^2) | ||
Space Complexity: O(n^2) | ||
For other explanations refer to: https://www.youtube.com/watch?v=_H8V5hJUGd0 | ||
""" | ||
|
||
|
||
def find_minimum_partitions(string: str) -> int: | ||
""" | ||
Returns the minimum cuts needed for a palindrome partitioning of string | ||
|
||
>>> find_minimum_partitions("aab") | ||
1 | ||
>>> find_minimum_partitions("aaa") | ||
0 | ||
>>> find_minimum_partitions("ababbbabbababa") | ||
3 | ||
""" | ||
n = len(string) | ||
cut = [0] * n | ||
ispalindrome = [[False for i in range(n)] for j in range(n)] | ||
for i in range(n): | ||
mincut = i | ||
for j in range(i + 1): | ||
if string[i] == string[j] and (i - j < 2 or ispalindrome[j + 1][i - 1]): | ||
ispalindrome[j][i] = True | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mincut = min(mincut, 0 if j == 0 else (cut[j - 1] + 1)) | ||
cut[i] = mincut | ||
return cut[n - 1] | ||
|
||
|
||
if __name__ == "__main__": | ||
s = input("Enter the string: ").strip() | ||
ans = find_minimum_partitions(s) | ||
print(f"Minimum number of partitions required for the string is {ans}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.