- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find length of longest palindromic substring after single rotation in Python

Suppose we have a string s, which we can rotate at any point exactly once. We have to find the length of the longest palindromic substring we can get by doing this operation.

So, if the input is like s = "elklev", then the output will be 7, as we can rotate between "el" and "klev" to get "levelk". So here the longest palinfromic substring length is 5.

To solve this, we will follow these steps −

s2 := concatenate s twice

max_len := 0

for x in range 0 to size of s − 1, do

for y in range 0 to size of s, do

temp := s2[from index x to x + y]

if temp is palindrome and size of temp > max_len, then

max_len := size of temp

return max_len

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, s): s2 = 2 * s max_len = 0 for x in range(len(s)): for y in range(len(s) + 1): temp = s2[x : x + y] if temp == temp[::−1] and len(temp) > max_len: max_len = len(temp) return max_len ob = Solution() s = "elklev" print(ob.solve(s))

## Input

"elklev"

## Output

5

- Related Articles
- Program to find length of longest palindromic substring in Python
- Program to find length of longest palindromic subsequence in Python
- Longest Palindromic Substring in Python
- Longest Palindromic Substring
- Program to find out the length of longest palindromic subsequence using Python
- Program to find length of longest consecutively increasing substring in Python
- Program to find length of longest repeating substring in a string in Python
- Program to find length of longest common substring in C++
- Program to find length of longest substring with 1s in a binary string after one 0-flip in Python
- Program to find length of longest substring with even vowel counts in Python
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find longest awesome substring in Python
- Program to find the length of longest substring which has two distinct elements in Python
- Program to find length of longest substring with character count of at least k in Python
- Program to find length of longest contiguously strictly increasing sublist after removal in Python

Advertisements