Open In App

Check if 2 * K + 1 non-empty strings exists whose concatenation forms the given string

Last Updated : 11 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S 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 strings AK, A(K – 1), A(K – 2), …, A1, and A0 is the string S. If found to be true, then print “Yes”. Otherwise, print “No”.

Examples:

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.

Input: S = “qwqwa”, K = 2
Output: No

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:

  • If the value of (2*K + 1) is greater than N, then print “No” and return from the function.
  • Otherwise, store the prefix of size K i.e., S[0, …, K] in a string A, and the suffix of size K i.e., S[N – K, …, N – 1] in a string B.
  • Reverse the string B and check if A is equal to B or not. If found to be true, then print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// 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
void checkString(string s, int k)
{
    // Stores the size of the string
    int n = s.size();
 
    // If n is less than 2*k+1
    if (2 * k + 1 > n) {
        cout << "No";
        return;
    }
 
    // Stores the first K characters
    string a = s.substr(0, k);
 
    // Stores the last K characters
    string b = s.substr(n - k, k);
 
    // Reverse the string
    reverse(b.begin(), b.end());
 
    // If both the strings are equal
    if (a == b)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    string S = "qwqwq";
    int K = 1;
    checkString(S, K);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
   
      // 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
    static void checkString(String s, int k)
    {
       
        // Stores the size of the string
        int n = s.length();
 
        // If n is less than 2*k+1
        if (2 * k + 1 > n) {
            System.out.println("No");
            return;
        }
 
        // Stores the first K characters
        String a = s.substring(0, k);
 
        // Stores the last K characters
        String b = s.substring(n - k, n);
 
        // Reverse the string
        StringBuffer str = new StringBuffer(b);
        // To reverse the string
        str.reverse();
        b = str.toString();     
       
        // If both the strings are equal
        if (a.equals(b))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
 
    // Driver Code
    public static void main (String[] args)
    {
        String S = "qwqwq";
        int K = 1;
        checkString(S, K);
    }
}
 
// This code is contributed by Dharanendra L V.


Python3




# Python3 program for the above approach
 
# Function to check if the S
# can be obtained by (K + 1) non-empty
# substrings whose concatenation and
# concatenation of the reverse
# of these K strings
def checkString(s, k):
   
    # Stores the size of the string
    n = len(s)
 
    # If n is less than 2*k+1
    if (2 * k + 1 > n):
        print("No")
        return
 
    # Stores the first K characters
    a = s[0:k]
 
    # Stores the last K characters
    b = s[n - k:n]
 
    # Reverse the string
    b = b[::-1]
 
    # If both the strings are equal
    if (a == b):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
    S = "qwqwq"
    K = 1
    checkString(S, K)
 
# This code is contributed by mohit kumar 29.


C#




// C# program for the above approach
using System;
class GFG {
 
    // 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
    static void checkString(string s, int k)
    {
       
        // Stores the size of the string
        int n = s.Length;
 
        // If n is less than 2*k+1
        if (2 * k + 1 > n) {
            Console.Write("No");
            return;
        }
 
        // Stores the first K characters
        string a = s.Substring(0, k);
 
        // Stores the last K characters
        string b = s.Substring(n - k, k);
 
        // Reverse the string
        char[] arr = b.ToCharArray();
        Array.Reverse(arr);
        b = new String(arr);
 
        // If both the strings are equal
        if (a == b)
            Console.Write("Yes");
        else
            Console.Write("No");
    }
 
    // Driver Code
    public static void Main()
    {
        string S = "qwqwq";
        int K = 1;
        checkString(S, K);
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
// Javascript 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
function checkString(s, k)
{
    // Stores the size of the string
    let n = s.length;
 
    // If n is less than 2*k+1
    if (2 * k + 1 > n) {
        document.write("No");
        return;
    }
 
    // Stores the first K characters
    let a = s.substr(0, k);
 
    // Stores the last K characters
    let b = s.substr(n - k, k);
 
    // Reverse the string
    b.split("").reverse().join("")
 
    // If both the strings are equal
    if (a == b)
        document.write("Yes");
    else
        document.write("No");
}
 
// Driver Code
    let S = "qwqwq";
    let K = 1;
    checkString(S, K);
 
// This code is contributed by gfgking.
</script>


Output: 

Yes

 

Time complexity: O(N) 
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads