Given a stringS consisting of N characters and positive integer K, the task is to check if there exist any (K + 1) strings i.e., A1, A2, A3, …, AK, A(K + 1)such that the concatenation of strings A1, A2, A3, …, AK, and A(K + 1) and the concatenation of the reverse of each stringsAK, A(K – 1), A(K – 2), …, A1, and A0 is the string S. If found to be true, then print “Yes”. Otherwise, print “No”.
Input: S = “qwqwq”, K = 1 Output: Yes Explanation: Consider the string A1 as “qw”, and A2 as “q”. Now the concatenation of A1, A2, reverse of A1 is “qwqwq”, which is the same as the given string S.
Approach: The given problem can be solved based on the observation that for a string S to satisfy the given condition, the first K characters must be equal to the last K characters of the given string. Follow the steps below to solve the problem:
Please Login to comment...