Minimum length of substring whose rotation generates a palindromic substring
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 repeated 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 the previous occurrence – 1).
- Return the minimum of all such lengths as the answer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_min_length(string s)
{
int hash[26];
int ans = INT_MAX;
for ( int i = 0; i < 26; i++)
hash[i] = -1;
for ( int i = 0; i < s.size(); i++) {
if (hash[s[i] - 'a' ] == -1)
hash[s[i] - 'a' ] = i;
else {
if (hash[s[i] - 'a' ] == i - 1
|| hash[s[i] - 'a' ] == i - 2)
return 0;
ans = min(ans,
i - hash[s[i] - 'a' ] - 1);
hash[s[i] - 'a' ] = i;
}
}
if (ans == INT_MAX)
return -1;
return ans;
}
int main()
{
string str = "abcdeba" ;
cout << count_min_length(str);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int count_min_length(String s)
{
int [] hash = new int [ 26 ];
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < 26 ; i++)
hash[i] = - 1 ;
for ( int i = 0 ; i < s.length(); i++)
{
if (hash[s.charAt(i) - 'a' ] == - 1 )
hash[s.charAt(i) - 'a' ] = i;
else
{
if (hash[s.charAt(i) - 'a' ] == i - 1 ||
hash[s.charAt(i) - 'a' ] == i - 2 )
return 0 ;
ans = Math.min(ans,
i - hash[s.charAt(i) - 'a' ] - 1 );
hash[s.charAt(i) - 'a' ] = i;
}
}
if (ans == Integer.MAX_VALUE)
return - 1 ;
return ans;
}
public static void main(String[] args)
{
String str = "abcdeba" ;
System.out.println(count_min_length(str));
}
}
|
Python3
import sys
INT_MAX = sys.maxsize;
def count_min_length(s):
hash = [ 0 ] * 26 ;
ans = sys.maxsize;
for i in range ( 26 ):
hash [i] = - 1 ;
for i in range ( len (s)):
if ( hash [ ord (s[i]) - ord ( 'a' )] = = - 1 ):
hash [ ord (s[i]) - ord ( 'a' )] = i;
else :
if ( hash [ ord (s[i]) - ord ( 'a' )] = = i - 1 or
hash [ ord (s[i]) - ord ( 'a' )] = = i - 2 ) :
return 0 ;
ans = min (ans, i - hash [ ord (s[i]) -
ord ( 'a' )] - 1 );
hash [ ord (s[i]) - ord ( 'a' )] = i;
if (ans = = INT_MAX):
return - 1 ;
return ans;
if __name__ = = "__main__" :
string = "abcdeba" ;
print (count_min_length(string));
|
C#
using System;
class GFG{
static int count_min_length( string s)
{
int [] hash = new int [26];
int ans = int .MaxValue;
for ( int i = 0; i < 26; i++)
hash[i] = -1;
for ( int i = 0; i < s.Length; i++)
{
if (hash[s[i] - 'a' ] == -1)
hash[s[i] - 'a' ] = i;
else
{
if (hash[s[i] - 'a' ] == i - 1 ||
hash[s[i] - 'a' ] == i - 2)
return 0;
ans = Math.Min(ans,
i - hash[s[i] - 'a' ] - 1);
hash[s[i] - 'a' ] = i;
}
}
if (ans == int .MaxValue)
return -1;
return ans;
}
public static void Main( string [] args)
{
string str = "abcdeba" ;
Console.WriteLine(count_min_length(str));
}
}
|
Javascript
<script>
function count_min_length(s) {
var hash = new Array(26).fill(0);
var ans = 2147483648;
for ( var i = 0; i < 26; i++)
hash[i] = -1;
for ( var i = 0; i < s.length; i++) {
if (hash[s[i].charCodeAt(0) - "a" .charCodeAt(0)] == -1)
hash[s[i].charCodeAt(0) - "a" .charCodeAt(0)] = i;
else {
if (
hash[s[i].charCodeAt(0) - "a" .charCodeAt(0)] == i - 1 ||
hash[s[i].charCodeAt(0) - "a" .charCodeAt(0)] == i - 2
)
return 0;
ans = Math.min(
ans,
i - hash[s[i].charCodeAt(0) - "a" .charCodeAt(0)] - 1
);
hash[s[i].charCodeAt(0) - "a" .charCodeAt(0)] = i;
}
}
if (ans === 2147483648) return -1;
return ans;
}
var str = "abcdeba" ;
document.write(count_min_length(str));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(26), as we are using extra space for hash.
Last Updated :
16 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...