Length of all prefixes that are also the suffixes of given string | C ++ program for the above approach ; Function to find the length of all prefixes of the given string that are also suffixes of the same string ; Stores the prefix string ; Traverse the string S ; Add the current character to the prefix string ; Store the suffix string ; Check if both the strings are equal or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSamePrefixSuffix ( string s , int n ) { string prefix = " " ; for ( int i = 0 ; i < n - 1 ; i ++ ) { prefix += s [ i ] ; string suffix = s . substr ( n - 1 - i , n - 1 ) ; if ( prefix == suffix ) { cout << prefix . size ( ) << " ▁ " ; } } } int main ( ) { string S = " ababababab " ; int N = S . size ( ) ; countSamePrefixSuffix ( S , N ) ; return 0 ; }