Minimum length of substring whose rotation generates a palindromic substring | Java Program to find the minimum length of substring whose rotation generates a palindromic substring ; Function to return the minimum length of substring ; Store the index of previous occurrence of the character ; Variable to store the maximum length of substring ; If the current character hasn 't appeared yet ; If the character has occured within one or two previous index , a palindromic substring already exists ; Update the maximum ; Replace the previous index of the character by the current index ; If character appeared at least twice ; Driver code
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 ) ) ; } }