Minimize length of a string by removing suffixes and prefixes of same characters | Function to find the minimum length of the string after removing the same characters from the end and front of the two strings after dividing into 2 substrings ; Initialize two pointers ; Traverse the string S ; Current char on left pointer ; Shift i towards right ; Shift j towards left ; Return the minimum possible length of string ; Driver Code
def minLength ( s ) : NEW_LINE INDENT i = 0 ; j = len ( s ) - 1 NEW_LINE while ( i < j and s [ i ] == s [ j ] ) : NEW_LINE INDENT d = s [ i ] NEW_LINE while ( i <= j and s [ i ] == d ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( i <= j and s [ j ] == d ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT return j - i + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aacbcca " NEW_LINE print ( minLength ( S ) ) NEW_LINE DEDENT