Check if 2 * K + 1 non-empty strings exists whose concatenation forms the given string
Last Updated :
11 May, 2021
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>
Time complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Reorder the given string to form a K-concatenated string Given a string S and an integer K. The task is to form a string T such that the string T is a reordering of the string S in a way that it is a K-Concatenated-String. A string is said to be a K-Concatenated-String if it contains exactly K copies of some string.For example, the string "geekgeek" is a
8 min read
Check whether given string can be generated after concatenating given strings Given three strings str, A and B. The task is to check whether str = A + B or str = B + A where + denotes concatenation. Examples: Input: str = "GeeksforGeeks", A = "Geeksfo", B = "rGeeks" Output: Yes str = A + B = "Geeksfo" + "rGeeks" = "GeeksforGeeks"Input: str = "Delhicapitals", B = "Delmi", C =
11 min read
Check if a string is concatenation of another given string Given two strings str1 and str2 of length N and M respectively, the task is to check if the string str1 can be formed by concatenating the string str2 repetitively or not. Examples: Input: str1 = âabcabcabcâ, str2 = âabcâOutput: YesExplanation: Concatenating the string str2 thrice generates the stri
7 min read
Generate a string whose all K-size substrings can be concatenated to form the given string Given a string str of size N and an integer K, the task is to generate a string whose substrings of size K can be concatenated to form the given string. Examples: Input: str = "abbaaa" K = 2 Output: abaa Explanation: All substring of size 2 of parent string "abaa" are "ab", "ba" and "aa". After conc
5 min read
Count subsequences 01 in string generated by concatenation of given numeric string K times Given a string S and a positive integer K, the task is to find the number of subsequences "01" in the string generated by concatenation of the given numeric string S K times. Examples: Input: S = "0171", K = 2Output: 6Explanation:The string formed by concatenation of S, K number of times is "0171017
6 min read
Longest substring of 0s in a string formed by k concatenations Given a binary string of length n and an integer k. Consider another string T which is formed by concatenating the given binary string k times. The task is to print the maximum size of a substring of T containing only zeroes. Examples: Input: str = 110010, k = 3 Output: 2 str = 110010 T = 1100101100
8 min read