@@ -1635,4 +1635,126 @@ public boolean repeatedSubstringPattern(String s) {
16351635 .collect (Collectors .groupingBy (Function .identity (), Collectors .counting ()));
16361636 return charCountMap .values ().stream ().anyMatch (val -> val <= 1 );
16371637 }
1638+
1639+ /**
1640+ * 5. Longest Palindromic Substring
1641+ *
1642+ * <pre>
1643+ * Given a string s, return the longest palindromic substring in s.
1644+ *
1645+ * Example 1:
1646+ * Input: s = "babad"
1647+ * Output: "bab"
1648+ * Explanation: "aba" is also a valid answer.
1649+ *
1650+ * Example 2:
1651+ * Input: s = "cbbd"
1652+ * Output: "bb"
1653+ * </pre>
1654+ *
1655+ * Constraints:
1656+ *
1657+ * <ul>
1658+ * <li>1 <= s.length <= 1000
1659+ * <li>s consist of only digits and English letters.
1660+ * </ul>
1661+ *
1662+ * <p>
1663+ * @see <a
1664+ * href="https://leetcode.com/problems/longest-palindromic-substring/
1665+ * solutions/4212564/beats-96-49-5-different-approaches-brute-force-eac-dp-ma-recursion/">
1666+ * Explained Manacher's Algorithm </a>
1667+ * @see <a href="https://www.youtube.com/watch?v=IvakBbsAhUU">Manacher's Algorithm (Video)</a>
1668+ * </p>
1669+ * @param str
1670+ * @return
1671+ */
1672+ public String longestPalindrome (String str ) {
1673+ if (str .length () <= 1 ) {
1674+ return str ;
1675+ }
1676+
1677+ int maxLen = 1 ;
1678+ String maxStr = str .substring (0 , 1 );
1679+ str = "#" + str .replaceAll ("" , "#" ) + "#" ;
1680+ System .out .println ("Reformed input:" + str );
1681+ int [] pal = new int [str .length ()];
1682+ int center = 0 ;
1683+ int right = 0 ;
1684+
1685+ for (int i = 0 ; i < str .length (); i ++) {
1686+ if (i < right ) {
1687+ pal [i ] = Math .min (right - i , pal [2 * center - i ]);
1688+ }
1689+
1690+ while (i - pal [i ] - 1 >= 0
1691+ && i + pal [i ] + 1 < str .length ()
1692+ && str .charAt (i - pal [i ] - 1 ) == str .charAt (i + pal [i ] + 1 )) {
1693+ pal [i ]++;
1694+ }
1695+
1696+ if (i + pal [i ] > right ) {
1697+ center = i ;
1698+ right = i + pal [i ];
1699+ }
1700+
1701+ if (pal [i ] > maxLen ) {
1702+ maxLen = pal [i ];
1703+ maxStr = str .substring (i - pal [i ], i + pal [i ] + 1 ).replaceAll ("#" , "" );
1704+ }
1705+ }
1706+
1707+ return maxStr ;
1708+ }
1709+
1710+ /**
1711+ * 1668. Maximum Repeating Substring
1712+ * <pre>
1713+ * For a string sequence, a string word is k-repeating if word concatenated k times is a
1714+ * substring of sequence. The word's maximum k-repeating value is the highest value k where
1715+ * word is k-repeating in sequence. If word is not a substring of sequence, word's maximum
1716+ * k-repeating value is 0.
1717+ *
1718+ * Given strings sequence and word, return the maximum k-repeating value of word in sequence.
1719+ *
1720+ * Example 1:
1721+ * Input: sequence = "ababc", word = "ab"
1722+ * Output: 2
1723+ * Explanation: "abab" is a substring in "ababc".
1724+ *
1725+ * Example 2:
1726+ * Input: sequence = "ababc", word = "ba"
1727+ * Output: 1
1728+ * Explanation: "ba" is a substring in "ababc". "baba" is not a substring in "ababc".
1729+ *
1730+ * Example 3:
1731+ * Input: sequence = "ababc", word = "ac"
1732+ * Output: 0
1733+ * Explanation: "ac" is not a substring in "ababc".
1734+ * </pre>
1735+ *
1736+ * Constraints:
1737+ * <ul>
1738+ * <li>1 <= sequence.length <= 100 </li>
1739+ * <li>1 <= word.length <= 100 </li>
1740+ * <li>sequence and word contains only lowercase English letters. </li>
1741+ * </ul>
1742+ *
1743+ * @param sequence
1744+ * @param word
1745+ * @return
1746+ */
1747+ public int maxRepeating (String sequence , String word ) {
1748+ int count = 0 ;
1749+ if (!sequence .contains (word )) return count ;
1750+ if (sequence .equals (word )) return ++count ;
1751+
1752+ while (sequence .contains (word )) {
1753+ count ++;
1754+ int idx = sequence .indexOf (word );
1755+ sequence = sequence .substring ( idx + (word .length ()- 1 ));
1756+ }
1757+ return count ;
1758+ }
1759+
16381760}
0 commit comments