Tag Archives: palindrome

Palindromic Tree | Introduction & Implementation

We encounter various problems like Maximum length palindrome in a string, number of palindromic substrings and many more interesting problems on palindromic substrings . Mostly of these palindromic substring problems have some DP O(n2) solution (n is length of the given string) or then we have a complex algorithm like Manacher’s algorithm which solves the… Read More »

Lexicographically first palindromic string

Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message “no palindromic string”. Examples: Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string Simple Approach: 1. Sort the string characters in alphabetical(ascending) order. 2. One be one find lexicographically… Read More »

N’th palindrome of K digits

Given two integers n and k, Find the lexicographical nth palindrome of k digits. Input : n = 5, k = 4 Output : 1441 Explanation: 4 digit lexicographical palindromes are: 1001, 1111, 1221, 1331, 1441 5th palindrome = 1441 Input : n = 4, k = 6 Output : 103301 Naive Approach A brute… Read More »

Make largest palindrome by changing at most K-digits

Given a string containing all digits, we need to convert this string to a palindrome by changing at most K digits. If many solutions are possible then print lexicographically largest one. Examples: Input : str = “43435” k = 3 Output : “93939” Lexicographically largest palindrome after 3 changes is “93939” Input : str =… Read More »

Print all palindromic partitions of a string

Given a string s, partition s such that every string of the partition is a palindrome. Return all possible palindrome partitioning of s. Example : Input : s = “bcc” Output : [[“b”, “c”, “c”], [“b”, “cc”]] Input : s = “geeks” Output : [[“g”, “e”, “e”, “k”, “s”], [“g”, “ee”, “k”, “s”]] We have… Read More »

Number of palindromic paths in a matrix

Given a matrix containing lower alphabetical characters only, we need to count number of palindromic paths in given matrix. A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell. Examples: Input : mat[][] =… Read More »