Maximum number of characters between any two same character in a string | Simple Java program to find maximum number of characters between two occurrences of same character ; Driver code
class GFG { static int maximumChars ( String str ) { int n = str . length ( ) ; int res = - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( str . charAt ( i ) == str . charAt ( j ) ) res = Math . max ( res , Math . abs ( j - i - 1 ) ) ; return res ; } public static void main ( String [ ] args ) { String str = " abba " ; System . out . println ( maximumChars ( str ) ) ; } }