Length of all prefixes that are also the suffixes of given string | Function to find the length of all prefixes of the given that are also suffixes of the same string ; Stores the prefix string ; Traverse the S ; Add the current character to the prefix string ; Store the suffix string ; Check if both the strings are equal or not ; Driver Code
def countSamePrefixSuffix ( s , n ) : NEW_LINE INDENT prefix = " " NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT prefix += s [ i ] NEW_LINE suffix = s [ n - 1 - i : 2 * n - 2 - i ] NEW_LINE if ( prefix == suffix ) : NEW_LINE INDENT print ( len ( prefix ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ababababab " NEW_LINE N = len ( S ) NEW_LINE countSamePrefixSuffix ( S , N ) NEW_LINE DEDENT