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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void checkString(String s, int k)
{
int n = s.length();
if ( 2 * k + 1 > n) {
System.out.println( "No" );
return ;
}
String a = s.substring( 0 , k);
String b = s.substring(n - k, n);
StringBuffer str = new StringBuffer(b);
str.reverse();
b = str.toString();
if (a.equals(b))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main (String[] args)
{
String S = "qwqwq" ;
int K = 1 ;
checkString(S, K);
}
}
|
Python3
def checkString(s, k):
n = len (s)
if ( 2 * k + 1 > n):
print ( "No" )
return
a = s[ 0 :k]
b = s[n - k:n]
b = b[:: - 1 ]
if (a = = b):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
S = "qwqwq"
K = 1
checkString(S, K)
|
C#
using System;
class GFG {
static void checkString( string s, int k)
{
int n = s.Length;
if (2 * k + 1 > n) {
Console.Write( "No" );
return ;
}
string a = s.Substring(0, k);
string b = s.Substring(n - k, k);
char [] arr = b.ToCharArray();
Array.Reverse(arr);
b = new String(arr);
if (a == b)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main()
{
string S = "qwqwq" ;
int K = 1;
checkString(S, K);
}
}
|
Javascript
<script>
function checkString(s, k)
{
let n = s.length;
if (2 * k + 1 > n) {
document.write( "No" );
return ;
}
let a = s.substr(0, k);
let b = s.substr(n - k, k);
b.split( "" ).reverse().join( "" )
if (a == b)
document.write( "Yes" );
else
document.write( "No" );
}
let S = "qwqwq" ;
let K = 1;
checkString(S, K);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...