Check whether given string can be generated after concatenating given strings
Last Updated :
12 Oct, 2022
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 = “capitals”
Output: No
Approach:
- If len(str) != len(A) + len(B) then it is not possible to generate str by concatenating a + b or b + a.
- Else check whether str starts with a and ends with b or it starts with b and ends with a. Print Yes if any of these is true else print No
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool startsWith(string str, string pre)
{
int strLen = str.length();
int preLen = pre.length();
int i = 0, j = 0;
while (i < strLen && j < preLen) {
if (str[i] != pre[j])
return false ;
i++;
j++;
}
return true ;
}
bool endsWith(string str, string suff)
{
int i = str.length() - 0;
int j = suff.length() - 0;
while (i >= 0 && j >= 0) {
if (str[i] != suff[j])
return false ;
i--;
j--;
}
return true ;
}
bool checkString(string str, string a, string b)
{
if (str.length() != a.length() + b.length())
return false ;
if (startsWith(str, a)) {
if (endsWith(str, b))
return true ;
}
if (startsWith(str, b)) {
if (endsWith(str, a))
return true ;
}
return false ;
}
int main()
{
string str = "GeeksforGeeks" ;
string a = "Geeksfo" ;
string b = "rGeeks" ;
if (checkString(str, a, b))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean startsWith(String str, String pre)
{
int strLen = str.length();
int preLen = pre.length();
int i = 0 , j = 0 ;
while (i < strLen && j < preLen)
{
if (str.charAt(i) != pre.charAt(j))
return false ;
i++;
j++;
}
return true ;
}
static boolean endsWith(String str, String suff)
{
int i = str.length() - 1 ;
int j = suff.length() - 1 ;
while (i >= 0 && j >= 0 )
{
if (str.charAt(i) != suff.charAt(j))
return false ;
i--;
j--;
}
return true ;
}
static boolean checkString(String str, String a, String b)
{
if (str.length() != a.length() + b.length())
return false ;
if (startsWith(str, a))
{
if (endsWith(str, b))
return true ;
}
if (startsWith(str, b))
{
if (endsWith(str, a))
return true ;
}
return false ;
}
public static void main(String args[])
{
String str = "GeeksforGeeks" ;
String a = "Geeksfo" ;
String b = "rGeeks" ;
if (checkString(str, a, b))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def startsWith( str , pre):
strLen = len ( str )
preLen = len (pre)
i = 0
j = 0
while (i < strLen and j < preLen):
if ( str [i] ! = pre[j]) :
return False
i + = 1
j + = 1
return True
def endsWith( str , suff):
i = len ( str ) - 1
j = len (suff) - 1
while (i > = 0 and j > = 0 ):
if ( str [i] ! = suff[j]):
return False
i - = 1
j - = 1
return True
def checkString( str , a, b):
if ( len ( str ) ! = len (a) + len (b)):
return False
if (startsWith( str , a)):
if (endsWith( str , b)):
return True
if (startsWith( str , b)):
if (endsWith( str , a)):
return True
return False
str = "GeeksforGeeks"
a = "Geeksfo"
b = "rGeeks"
if (checkString( str , a, b)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static Boolean startsWith(String str,
String pre)
{
int strLen = str.Length;
int preLen = pre.Length;
int i = 0, j = 0;
while (i < strLen && j < preLen)
{
if (str[i] != pre[j])
return false ;
i++;
j++;
}
return true ;
}
static Boolean endsWith(String str,
String suff)
{
int i = str.Length - 1;
int j = suff.Length - 1;
while (i >= 0 && j >= 0)
{
if (str[i] != suff[j])
return false ;
i--;
j--;
}
return true ;
}
static Boolean checkString(String str,
String a,
String b)
{
if (str.Length != a.Length + b.Length)
return false ;
if (startsWith(str, a))
{
if (endsWith(str, b))
return true ;
}
if (startsWith(str, b))
{
if (endsWith(str, a))
return true ;
}
return false ;
}
public static void Main(String []args)
{
String str = "GeeksforGeeks" ;
String a = "Geeksfo" ;
String b = "rGeeks" ;
if (checkString(str, a, b))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function startsWith( $str , $pre )
{
$strLen = strlen ( $str );
$preLen = strlen ( $pre );
$i = 0; $j = 0;
while ( $i < $strLen && $j < $preLen )
{
if ( $str [ $i ] != $pre [ $j ])
return false;
$i ++;
$j ++;
}
return true;
}
function endsWith( $str , $suff )
{
$i = strlen ( $str )- 0;
$j = strlen ( $suff )- 0;
while ( $i >= 0 && $j >= 0)
{
if ( $str [ $i ] != $suff [ $j ])
return false;
$i --;
$j --;
}
return true;
}
function checkString( $str , $a , $b )
{
if ( strlen ( $str ) != strlen ( $a ) + strlen ( $b ))
return false;
if (startsWith( $str , $a ))
{
if (endsWith( $str , $b ))
return true;
}
if (startsWith( $str , $b ))
{
if (endsWith( $str , $a ))
return true;
}
return false;
}
$str = "GeeksforGeeks" ;
$a = "Geeksfo" ;
$b = "rGeeks" ;
if (checkString( $str , $a , $b ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function startsWith(str, pre)
{
let strLen = str.length;
let preLen = pre.length;
let i = 0, j = 0;
while (i < strLen && j < preLen)
{
if (str[i] != pre[j])
return false ;
i++;
j++;
}
return true ;
}
function endsWith(str, suff)
{
let i = str.length - 1;
let j = suff.length - 1;
while (i >= 0 && j >= 0)
{
if (str[i] != suff[j])
return false ;
i--;
j--;
}
return true ;
}
function checkString(str, a, b)
{
if (str.length != a.length + b.length)
return false ;
if (startsWith(str, a))
{
if (endsWith(str, b))
return true ;
}
if (startsWith(str, b))
{
if (endsWith(str, a))
return true ;
}
return false ;
}
let str = "GeeksforGeeks" ;
let a = "Geeksfo" ;
let b = "rGeeks" ;
if (checkString(str, a, b))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(max(a,b,c)), as we are using a loop to traverse a, b, and c times. Where a, b, and c are the length of the strings.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...