|
1 | 1 | package com.thealgorithms.dynamicprogramming;
|
2 | 2 |
|
3 |
| -import java.util.Scanner; |
4 |
| - |
5 | 3 | /**
|
6 |
| - * @file @brief Implements [Palindrome |
7 |
| - * Partitioning](https://leetcode.com/problems/palindrome-partitioning-ii/) |
8 |
| - * algorithm, giving you the minimum number of partitions you need to make |
| 4 | + * Provides functionality to solve the Palindrome Partitioning II problem, which involves finding |
| 5 | + * the minimum number of partitions needed to divide a given string into palindromic substrings. |
| 6 | + * |
| 7 | + * <p> |
| 8 | + * The problem is solved using dynamic programming. The approach involves checking all possible |
| 9 | + * substrings and determining whether they are palindromes. The minimum number of cuts required |
| 10 | + * for palindrome partitioning is computed in a bottom-up manner. |
| 11 | + * </p> |
| 12 | + * |
| 13 | + * <p> |
| 14 | + * Example: |
| 15 | + * <ul> |
| 16 | + * <li>Input: "nitik" => Output: 2 (Partitioning: "n | iti | k")</li> |
| 17 | + * <li>Input: "ababbbabbababa" => Output: 3 (Partitioning: "aba | b | bbabb | ababa")</li> |
| 18 | + * </ul> |
| 19 | + * </p> |
9 | 20 | *
|
10 |
| - * @details palindrome partitioning uses dynamic programming and goes to all the |
11 |
| - * possible partitions to find the minimum you are given a string and you need |
12 |
| - * to give minimum number of partitions needed to divide it into a number of |
13 |
| - * palindromes [Palindrome Partitioning] |
14 |
| - * (https://www.geeksforgeeks.org/palindrome-partitioning-dp-17/) overall time |
15 |
| - * complexity O(n^2) For example: example 1:- String : "nitik" Output : 2 => "n |
16 |
| - * | iti | k" For example: example 2:- String : "ababbbabbababa" Output : 3 => |
17 |
| - * "aba | b | bbabb | ababa" |
18 |
| - * @author [Syed] (https://github.com/roeticvampire) |
| 21 | + * @see <a href="https://leetcode.com/problems/palindrome-partitioning-ii/">Palindrome Partitioning II</a> |
| 22 | + * @see <a href="https://www.geeksforgeeks.org/palindrome-partitioning-dp-17/">Palindrome Partitioning (GeeksforGeeks)</a> |
19 | 23 | */
|
20 | 24 | public final class PalindromicPartitioning {
|
21 | 25 | private PalindromicPartitioning() {
|
22 | 26 | }
|
23 | 27 |
|
24 |
| - public static int minimalpartitions(String word) { |
| 28 | + public static int minimalPartitions(String word) { |
25 | 29 | int len = word.length();
|
26 | 30 | /* We Make two arrays to create a bottom-up solution.
|
27 | 31 | minCuts[i] = Minimum number of cuts needed for palindrome partitioning of substring
|
@@ -76,15 +80,4 @@ public static int minimalpartitions(String word) {
|
76 | 80 | // string. i.e., str[0..n-1]
|
77 | 81 | return minCuts[len - 1];
|
78 | 82 | }
|
79 |
| - |
80 |
| - public static void main(String[] args) { |
81 |
| - Scanner input = new Scanner(System.in); |
82 |
| - String word; |
83 |
| - System.out.println("Enter the First String"); |
84 |
| - word = input.nextLine(); |
85 |
| - // ans stores the final minimal cut count needed for partitioning |
86 |
| - int ans = minimalpartitions(word); |
87 |
| - System.out.println("The minimum cuts needed to partition \"" + word + "\" into palindromes is " + ans); |
88 |
| - input.close(); |
89 |
| - } |
90 | 83 | }
|
0 commit comments