Given a string str, the task is to find the minimum length of substring required to rotate that generates a palindromic substring from the given string.
Examples:
Input: str = “abcbd”
Output: 0
Explanation: No palindromic substring can be generated there is no repeating character in the string.Input: str = “abcdeba”
Output: 3
Explanation: Rotate substring “deb” to convert the given string to abcbeda with a palindromic substring “bcb”.
Approach:
- If no character repeats in the string, then no palindromic substring can be generated.
- For every repeating character, check if the index of its previous occurrence is within one or two indices from the current index. If so, then a palindromic substring already exists.
- Otherwise, calculate the length of (current index – index of previous occurrence – 1).
- Return the minimum of all such lengths as the answer
Below is the implementation of the above approach:
C++
// C++ Program to find the minimum // length of substring whose rotation // generates a palindromic substring #include <bits/stdc++.h> using namespace std; // Function to return the // minimum lenth of substring int count_min_length(string s) { // Store the index of // previous occurrence // of the character int hash[26]; // Variable to store // the maximum length // of substring int ans = INT_MAX; for (int i = 0; i < 26; i++) hash[i] = -1; for (int i = 0; i < s.size(); i++) { // If the current character // hasn't appeared yet if (hash[s[i] - 'a'] == -1) hash[s[i] - 'a'] = i; else { // If the character has occured // within one or two previous // index, a palindromic substring // already exists if (hash[s[i] - 'a'] == i - 1 || hash[s[i] - 'a'] == i - 2) return 0; // Update the maximum ans = min(ans, i - hash[s[i] - 'a'] - 1); // Replace the previous // index of the character by // the current index hash[s[i] - 'a'] = i; } } // If character appeared // at least twice if (ans == INT_MAX) return -1; return ans; } // Driver Code int main() { string str = "abcdeba"; cout << count_min_length(str); } |
Java
// Java Program to find the minimum // length of substring whose rotation // generates a palindromic substring import java.util.*; import java.lang.*; class GFG{ // Function to return the // minimum lenth of substring static int count_min_length(String s) { // Store the index of // previous occurrence // of the character int[] hash = new int[26]; // Variable to store // the maximum length // of substring int ans = Integer.MAX_VALUE; for (int i = 0; i < 26; i++) hash[i] = -1; for (int i = 0; i < s.length(); i++) { // If the current character // hasn't appeared yet if (hash[s.charAt(i) - 'a'] == -1) hash[s.charAt(i) - 'a'] = i; else { // If the character has occured // within one or two previous // index, a palindromic substring // already exists if (hash[s.charAt(i) - 'a'] == i - 1 || hash[s.charAt(i) - 'a'] == i - 2) return 0; // Update the maximum ans = Math.min(ans, i - hash[s.charAt(i) - 'a'] - 1); // Replace the previous // index of the character by // the current index hash[s.charAt(i) - 'a'] = i; } } // If character appeared // at least twice if (ans == Integer.MAX_VALUE) return -1; return ans; } // Driver code public static void main(String[] args) { String str = "abcdeba"; System.out.println(count_min_length(str)); } } // This code is contributed by offbeat |
Python3
# Python3 program to find the minimum # length of substring whose rotation # generates a palindromic substring import sys INT_MAX = sys.maxsize; # Function to return the # minimum lenth of substring def count_min_length(s): # Store the index of # previous occurrence # of the character hash = [0] * 26; # Variable to store # the maximum length # of substring ans = sys.maxsize; for i in range(26): hash[i] = -1; for i in range(len(s)): # If the current character # hasn't appeared yet if (hash[ord(s[i]) - ord('a')] == -1): hash[ord(s[i]) - ord('a')] = i; else : # If the character has occured # within one or two previous # index, a palindromic substring # already exists if (hash[ord(s[i]) - ord('a')] == i - 1 or hash[ord(s[i]) - ord('a')] == i - 2) : return 0; # Update the maximum ans = min(ans, i - hash[ord(s[i]) - ord('a')] - 1); # Replace the previous # index of the character by # the current index hash[ord(s[i]) - ord('a')] = i; # If character appeared # at least twice if (ans == INT_MAX): return -1; return ans; # Driver Code if __name__ == "__main__": string = "abcdeba"; print(count_min_length(string)); # This code is contributed by AnkitRai01 |
C#
// C# Program to find the minimum // length of substring whose rotation // generates a palindromic substring using System; class GFG{ // Function to return the // minimum lenth of substring static int count_min_length(string s) { // Store the index of // previous occurrence // of the character int[] hash = new int[26]; // Variable to store // the maximum length // of substring int ans = int.MaxValue; for (int i = 0; i < 26; i++) hash[i] = -1; for (int i = 0; i < s.Length; i++) { // If the current character // hasn't appeared yet if (hash[s[i] - 'a'] == -1) hash[s[i] - 'a'] = i; else { // If the character has occured // within one or two previous // index, a palindromic substring // already exists if (hash[s[i] - 'a'] == i - 1 || hash[s[i] - 'a'] == i - 2) return 0; // Update the maximum ans = Math.Min(ans, i - hash[s[i] - 'a'] - 1); // Replace the previous // index of the character by // the current index hash[s[i] - 'a'] = i; } } // If character appeared // at least twice if (ans == int.MaxValue) return -1; return ans; } // Driver code public static void Main(string[] args) { string str = "abcdeba"; Console.WriteLine(count_min_length(str)); } } // This code is contributed by AnkitRai01 |
3
Time Complexity: O(N)
Recommended Posts:
- Left Rotation and Right Rotation of a String
- Check if concatenation of any permutation of given list of arrays generates the given array
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Maximum length palindromic substring such that it starts and ends with given char
- Make palindromic string non-palindromic by rearranging its letters
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Minimum size substring to be removed to make a given string palindromic
- Minimum characters to be replaced to make a string concatenation of a K-length palindromic string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Lexicographically minimum string rotation | Set 1
- Minimum number of flips with rotation to make binary string alternating
- Count of pairs of strings whose concatenation forms a palindromic string
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


