Check if 2 * K + 1 non | C ++ program for the above approach ; Function to check if the string S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkString ( string s , int k ) { int n = s . size ( ) ; if ( 2 * k + 1 > n ) { cout << " No " ; return ; } string a = s . substr ( 0 , k ) ; string b = s . substr ( n - k , k ) ; reverse ( b . begin ( ) , b . end ( ) ) ; if ( a == b ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " qwqwq " ; int K = 1 ; checkString ( S , K ) ; return 0 ; }